Event broadcasting with streams

Every websocket is a stream, which is super. (Every tcp connection is a stream too, so its not a ws specific problem.) When a user connects to my WS I can start and control a stream. These streams in my case only go from the server to the client, the other direction is not interesting. When the stream is opened I know exactly what topics are interesting for the given client. And I have an actor who gets messages when a topic is modified.
What I want to somehow connect the WS with this actor, so when the actor gets the modified message all of the connected and “subscribed” clients get notified.

Is there any premade/easy-to-use/best practice for that?

What I know so far:
Using Source.queue is nice, but with that, I somehow need to manage the connection and leaving part manually.
Source.actorRef has the same problems as the Source.queue but at least I can subscribe to the given actors lifecycle events.
BroadcastHub seems to be the overall winner, but with this, I still have the question, what is the better if the topics have a separated hub, or if I have the same hub, and filter the messages? (The second option seems a waste but I have no experience with this yet so asking is simpler :D )

Probably I could solve the problem with any of the 4 mentioned way, but I would like to know what is the best, and if sb has real-life experience with this, what worked and what was bad?

1 Like

Not easy to talk about an obvious choice that is better than others without knowing more about the problem being solved, for example if you have some stateful thing going on with the topic (chat room with a status for example) would fit an actor vs something that only is about pub-sub perhaps suited better with the BCHub.

Sorry to not reply! The BroadcastHub was the way to go, worked perfectly, thanks!

1 Like