Live data from Hacker News

Server-Sent Events (SSE) Are Underrated

igorstechnoclub.com

91–100 of 153 posts

Re: Server-Sent Events (SSE) Are Underrated

#92

It doesn’t mention the big drawback of SSE as spelled out in the MDN docs: “Warning: When not used over HTTP/2, SSE suffers from a limitation to the maximum number of open connections, which can be especially painful when opening multiple tabs, as the limit is per browser and is set to a very low number (6).”

One of my company's APIs uses SSE, and it's been a big support headache for us, because many people are being corporate firewalls that don't do HTTP/2 or HTTP/3, and people often open many tabs at the same time. It's unfortunately not possible to detect client-side whether the limit has been reached. Another drawback of SSE is lack of authorization header support. There are a few polyfills (like this one [1]) that si…

I hate to suggest a solution before testing it myself so apologies in advance but I have a hunch that Broadcast Channel API can help you detect browser tab opens on client side. New tabs won't connect to event source and instead listen for localStorage updates that the first loaded tab makes.

https://www.google.com/search?q=can+I+use+BroadcastChannel+A...

The problem in this case is how to handle the first tab closing and re-assign which tab then becomes the new "first" tab that connects to the event source but it may be a LOE to solve.

Again apologies for suggesting unproven solutions but at the same time I'm interested in feedback it gets to see if its near the right track

Re: Server-Sent Events (SSE) Are Underrated

#93
post #48

Earlier quoted context omitted.

Send a dummy event and see if you get an ACK in response. Depends on the library you're using.

There's no ack on a raw SSE stream, unfortunately -- unless you mean send an event and expect the client to issue an HTTP request to the server like a keepalive?

There should be an ACK on the tcp packet (IIRC it’s not a lateral ACK but something like it) and the server should handle a timeout on that as the connection being “closed” which can be returned to the connection opener.

You might want to look into timeouts or error callbacks on your connection library/framework.

Re: Server-Sent Events (SSE) Are Underrated

#95
post #46
post #22

Earlier quoted context omitted.

Generally you're going to want to send ping events pretty regularly (I'd default to every 15-30 seconds depending on application) whether you're using SSE, WebSockets, or something else. Otherwise if the server crashes the client might not know the connection is no longer live.

What do you do for mobile phones: using data/radio for pings would kill the battery? After locking the phone, how is the ping restarted when the phone is unlocked? Or backgrounding the browser/app?

The way I've implemented SSE is to make use of the fact it can also act like HTTP long-polling when the GET request is initially opened. The SSE events can be given timestamps or UUIDs and then subsequent requests can include the last received ID or the time of the last received event, and request the SSE endpoint replay events up until the current time.

You could also add a ping with a client-requestable interval, e.g. 30 seconds (for foreground app) and 5 minutes or never (for backgrounded app), so the TCP connection is less frequently going to cause wake events when the device is idle. As client, you can close and reopen your connection when you choose, if you think the TCP connection is dead on the other side or you want to reopen it with a new ping interval.

Tradeoff of `?lastEventId=` - your SSE serving thing needs to keep a bit of state, like having a circular buffer of up to X hours worth of events. Depending on what you're doing, that may scale badly - like if your SSE endpoint is multiple processes behind a round-robin load balancer... But that's a problem outside of whether you're choosing to use SSE, websockets or something else.

To be honest, if you're worrying about mobile drain, the most battery efficient thing I think anyone can do is admit defeat and use one of the vendor locked-in things like firebase (GCM?) or apple's equivalent notification things: they are using protocols which are more lightweight than HTTP (last I checked they use XMPP same as whatsapp?), can punch through firewalls fairly reliably, batch notifications from many apps together so as to not wake devices too regularly, etc etc...

Having every app keep their own individual connections open to receive live events from their own APIs sucks battery in general, regardless of SSE or websockets being used.

Re: Server-Sent Events (SSE) Are Underrated

#97

It doesn’t mention the big drawback of SSE as spelled out in the MDN docs: “Warning: When not used over HTTP/2, SSE suffers from a limitation to the maximum number of open connections, which can be especially painful when opening multiple tabs, as the limit is per browser and is set to a very low number (6).”

Http2 is controllable by you, since it's supposed in every browser. So, the way to fix this limitation is to use http2

Yes, use a proper load balancer that can do that. And use Http3 which is also supported by all relevant browsers at this point. There's no good reason to build new things on top of old things.

Re: Server-Sent Events (SSE) Are Underrated

#98
SSE is not underrated. In fact it's being used by Open AI for streaming completions. It's just not always needed unlike the very obvious use cases for normal REST APIs and Websockets.

It was a pain to figure out how to get it to work in a ReactJS codebase I was working on then and from what I remember Axios didn't support it then so I had to use native fetch to get it to work.

Re: Server-Sent Events (SSE) Are Underrated

#99
post #66
post #50

Earlier quoted context omitted.

This, it works well in a service worker for example.

How does this work with a service worker? I've only managed to do this via SharedWorker (which is not available on Chrome on Android).

You can just open a stream in the service worker and push events via postMessage and friends.

Another nice thing to do is to wire up a simple filesystem monitor for all your cached assets that pushes path & timestamp events to the service worker whenever they change, then the service worker can refresh affected clients too (with only a little work this is endgame livereload if you’re not constrained by your environment)

Re: Server-Sent Events (SSE) Are Underrated

#100
post #85
post #70

Earlier quoted context omitted.

From what I understand websockets are great until you have to load balance them. And then you learn why they aren’t so great.

I've scaled websockets before, it isn't that hard. You need to scale up before your servers become overloaded, and basically new connections go north to the newly brought up server. It is a different mentality than scaling stateless services but it isn't super duper hard.

Can you suggest some resources to learn more about Websocket scaling? Seems like an interesting topic
Post reply on HN