I have an actor which is launched with application, running in the background watching for certain changes and if there are any reporting them. At the moment it just a println
to the console. What I need to do is whenever there is a new message - send it to the front end using Websocket.
This is my Play Global object where the monitoring/listening actor is launched:
object Global extends GlobalSettings {
override def onStart(app: Application) {
class Listener extends Actor {
//This needs to be changed to pass messages to Websocket, how?
def receive = {
case Create(path) => println("CREATE " + path)
case Delete(path) => println("DELETE " + path)
case Modify(path) => println("MODIFY " + path)
}
}
val listener = Akka.system.actorOf(Props[Listener], "listener")
val swatch = Akka.system.actorOf(Props[SwatchActor], "swatch")
swatch ! Watch("/folder/path", Seq(Create, Modify, Delete), true, Some(listener))
}
}
This is my Play controller:
object Application extends Controller {
def test = WebSocket.using[String] { request =>
//This hopefully gets the listener actor reference?
val listener = Akka.system.actorSelection("/user/listener")
val (out, channel) = Concurrent.broadcast[String]
val in = Iteratee.foreach[String] { msg =>
//Actor messages must be pushed here, how?
channel push("RESPONSE: " + msg)
}
(in, out)
}
}
I understand that in order for websocket connection to be established there has to be an initial "in".
So my problems are:
- How do I modify the
Listener
actor to push messages to Websocket? - What do I need to do to prepare the actor to push messages once the websocket connection is established?
- How do I push messages from the listener actor to the websocket?