I need to send a private response to the client for which I'm currently handling a Play WebSocket message, while for other messages I need to broadcast responses to all clients.
In the Play chat example, incoming messages are immediately offloaded to a single Actor:
case Connected(enumerator) =>
// Create an Iteratee to consume the feed, assuming all requests have
// a JSON "text" node, and delegate all to the Akka Actor:
val iteratee = Iteratee.foreach[JsValue] { event =>
default ! Talk(username, (event \ "text").as[String])
}.map { _ =>
default ! Quit(username)
}
(iteratee,enumerator)
Above, once the connection is approved enumerator
is passed back, which refers to the single chatEnumerator
that was already created by that same Actor:
val (chatEnumerator, chatChannel) = Concurrent.broadcast[JsValue]
I assume this does not allow me to send a message to just a single client? Should I remove the broadcast functionality and create and keep track of enumerators for each client, and then iterate myself? Or can I somehow get a reference to a client-specific enumerator in the foreach
?
(I understand that built-in flexibility depends on the implementation, but this use case seems quite common to me. Like when using WebSockets with Socket.IO, I can easily send messages to either all clients, all clients except the sender of the current request, or just a single client. That's what I'm trying to achieve in Play 2.1.x as well.)