Also, another day, another mostly AI-written article on HN's top page :)
Server-Sent Events (SSE) Are Underrated
91–100 of 153 posts
Re: Server-Sent Events (SSE) Are Underrated
#92It 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…
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
#93Earlier 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?
You might want to look into timeouts or error callbacks on your connection library/framework.
Re: Server-Sent Events (SSE) Are Underrated
#94Can Django with vanilla gunicorn do this ?
Re: Server-Sent Events (SSE) Are Underrated
#95Earlier 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?
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
#96Re: Server-Sent Events (SSE) Are Underrated
#97It 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
Re: Server-Sent Events (SSE) Are Underrated
#98It 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
#99Earlier 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).
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
#100Earlier 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.