Identify WebSocket connection to filter out broadcasted events

Hi all,
I want to implement a simple chat application with a WebSocket interface and can’t figure out how to use Akka HTTP API to get some identifier representing websocket connection to use it later for filtering broadcasted messages and deliver to the client with the particular identifier.

As I understand, I should wrap a userFlow (constructed using Flow.fromSinkAndSource(incoming, outgoing)) with handleWebSocketMessages:

val wsChatRoute =
     path("ws-chat") {
       handleWebSocketMessages(userFlow)
     }

and outgoing must contain a code for filtering broadcasted messages by connection to deliver right messages to it, so userFlow must be a function on connection - userFlow(connection). But how to achieve this, how I can connection object on the server’s side?

Thanks!

2 Likes

I wrote a few minimal websocket samples a few years ago, might be useful for inspiration (even if they don’t do any filtering):


The first one represents a chat room with an actor and may be easier to adapt to filtering in some way, even if you could probably achieve the same with some stream statefulness as well.

1 Like

Johan, thank you for reply and examples!

Actually, I have almost the same code as in your second example, but your first example gave me an idea of a possible solution.

Thanks!