Question

I use Play Framework 2.2.1 with Scala.

The scenario I want to achieve is:

As soon as a new registered user logs in the for the first time, some interesting updates must be made into the database.
It would be a pretty long-running process (about few seconds).
What I want is to notify the user (front-end so) when all the updates are made, and that he can now benefit of these newly created links (no matter what the updates could bring to him in details).

I've thought about detaching an Akka actor in my controller in order to process the needed logic for the different updates and execute them into the database.
Indeed, the database access is a blocking IO in my case.

My main question is how to benefit of websocket to notify the user only when this long-running task is completed.

Does a mechanism exist to notify my already established websocket connection that an actor has just finished its job ?

Was it helpful?

Solution

Maybe something like this?:

In your controller, when the user first makes the request, call the method that is going to execute the different operations, since these are blocking, wrap the DB calls in a given actor, then instead of calling a simple method to get the result, you should be able to ask the actor to do the operations, this ask method (cf Akka doc) will return a future.

This future would then be sent to a given actor that is holding references to the different connections (meanwhile the controller returns a Result, Ok...), this actor should then listen for the state of the future, in case it's successful (onSuccess), push a message using the appropriate connection to the appropriate user. The out enumerator can be retrieved and saved when first creating the WS connection, a basic approach that comes to mind is that you can keep them in a map with the user id as key. You can then use these enumerators to push messages to the clients.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top