Search code examples
architecturesoftware-designserver-sent-events

How to send message to concrete user id using server-sent events?


Does server-sent events allow to send message to concrete user by user id? Or it works like fanout?


Solution

  • Sending to the current user is the only choice; it is not a broadcast technology and fan-out is not an option.

    Each SSE connection is a dedicated socket for each client. (WebSocket is too.) So if you want to broadcast to more than one client you need to perform extra steps on the server-side (e.g. shared memory, or each process polling a database, etc.)

    If you want to send a message to a specific user id then it is basically the same. E.g. have one DB record (or shared memory slot) for each user id, and store the message there. If that user id has a SSE connection the server-side process would be polling that particular DB record. Each time it changes it pushes out the new message to the client.