1

I have 5 different types of notifications A, B, C, D and E to be sent to the user. There is no relationship among A, B, C, D, and E.

To give you an idea, A could be facebook like, B could be new friend request etc.

I would like to know if it is ok to use a single SSE connection for all these notifications or am I supposed to maintain a connection for every event type?

Can you share some best practices around this?

RamPrakash
  • 2,218
  • 3
  • 25
  • 54

1 Answers1

1

Use a single SSE connection.

The normal limit on scaling are the socket that an SSE connection uses. So it is better that you have just a single connection. You won't end up with any more logic or complexity on the client-side: a single message handler that calls a function based on the message type (A/B/C/D/E), vs. five message handlers.

There are also some browser limits on having a lot of SSE connections open to the same domain, so a single connection avoids hitting any of them.

If using HTTP/2 you could argue that the multiplexing means there will actually only be a single socket connection. But in the absence of any strong reason to have multiple sockets, I would still always go with a single SSE connection, even over HTTP/2.

Darren Cook
  • 27,837
  • 13
  • 117
  • 217
  • how does one use a single SSE connection if the URL contains parameters? For instance, suppose I have `EventSource(https://example.com/endpoint?name=me)` for a variety of different names; what would you recommend? – Rylan Schaeffer Jun 05 '22 at 06:12
  • 1
    @RylanSchaeffer Send all the parameters at the same time. If that is not practical, you will need something more complicated (E.g. stop old connection and restart it with new parameters; one connection each; send list of subscriptions in separate ajax requests; use websockets, etc, etc.) – Darren Cook Jun 05 '22 at 08:08
  • If I have multiple parameters that the user inputs at different times (in a React app), what would you recommend? A websocket? Right now I'm trying to use multiple `EventSources`, every time a new input is entered. – Rylan Schaeffer Jun 05 '22 at 14:59
  • 1
    @RylanSchaeffer It depends. :-) On what the design goals are, and if your current approach is satisfying them or not. Definitely too complicated to handle in a comment. And possibly too specific to your own set of circumstances to make a good new question. – Darren Cook Jun 05 '22 at 18:23
  • can I ask you to take a look at my question? https://stackoverflow.com/questions/72512296/reactjs-why-does-navigating-appear-to-change-readystate-of-previous-eventsou – Rylan Schaeffer Jun 06 '22 at 01:57