News for hackers. Stuff that matters.
Re:Server Sent Events by mort96 on Tuesday May 16, 2023 @03:42AM (#35958765)
I don't think I understand. Both WebSockets and server-sent events are just a long lived TCP socket, right? Why would SSE be lower overhead? Why wouldn't it be more overhead to go through the whole HTTP request handling machinery for EVRY chat message everyone sends? You already have an underlying 2-way connection in the TCP socket, why not use it?

And why would latency be lower with HTTP requests + SSE than with websockets?

Re:Server Sent Events by jongjong on Tuesday May 16, 2023 @05:04AM (#35959276)
It's not. SSE has higher overhead and there are browser limits as to how many concurrent channels a single client can consume. Also, SSE encourages you to shift complexity to the back end where it would be more appropriate on the front end.

For example, the client knows which channels it needs, so why not let the client decide which channels to subscribe to over a single WebSocket connection? With SSE, you end up having to do this channel management and keep track of which user is associated with what SSE connections on the back end since the user cannot just subscribe to channels on-demand. If your system runs on multiple processes or hosts, figuring out which process/host a particular user is connected to is a nightmare and forces you to create clients on the back end to aggregate messages for specific users and then publish to the correct SSE connection. It moves all this domain logic to the back end which doesn't need to exist and complicates access control.

I have no idea why this "let's micromanage everything data stream for each user individually on the back end" approach became so popular while the alternative approach of end-to-end pub/sub is almost never used when it results in a much simpler, more secure system.

Re:Server Sent Events by cassepipe on Tuesday May 16, 2023 @05:52AM (#35959611)
In NestJs it is quite easy to manage in the backend, there's a SSE decorator for you SSE service and then you just return an observable on a subject. (See RxJs) Sending message events sends to all observers on the subject and when the `observed` property` becomes false, you just close the connection.