Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I use a single Play WebSocket for both broadcasting and private messages?
    primarykey
    data
    text
    <p>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.</p> <p>In the Play chat example, incoming messages <a href="https://github.com/playframework/playframework/blob/master/samples/scala/websocket-chat/app/models/ChatRoom.scala" rel="nofollow noreferrer">are immediately offloaded to a single Actor</a>:</p> <pre><code>case Connected(enumerator) =&gt; // 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 =&gt; default ! Talk(username, (event \ "text").as[String]) }.map { _ =&gt; default ! Quit(username) } (iteratee,enumerator) </code></pre> <p>Above, once the connection is approved <code>enumerator</code> is passed back, which refers to the single <code>chatEnumerator</code> that was already created by that same Actor:</p> <pre><code>val (chatEnumerator, chatChannel) = Concurrent.broadcast[JsValue] </code></pre> <p>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 <code>foreach</code>?</p> <p>(I understand that built-in flexibility <a href="https://stackoverflow.com/questions/15469659/private-message-using-websockets/15474698#15474698">depends on the implementation</a>, but this use case seems quite common to me. Like when using WebSockets with <a href="http://socket.io/" rel="nofollow noreferrer">Socket.IO</a>, I can easily send messages <a href="https://stackoverflow.com/questions/10058226/send-response-to-all-clients-except-sender-socket-io/10099325#10099325">to either</a> 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.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload