Live data from Hacker News

Server-Sent Events (SSE) Are Underrated

igorstechnoclub.com

1–10 of 153 posts

Re: Server-Sent Events (SSE) Are Underrated

#2
https://data-star.dev is a hypermedia-oriented front end library built entirely around the idea of streaming hypermedia responses via SSE.

It was developed using Go & NATS as backend technologies, but works with any SSE implementation.

Worth checking out if you want to explore SSE and what can be achieved w/it more deeply. Here is an interview with the author:

https://www.youtube.com/watch?v=HbTFlUqELVc

Re: Server-Sent Events (SSE) Are Underrated

#3
They're underrated when they work™

Currently at work I'm having issues because - Auth between an embedded app and javascript's EventSource is not working, so I have to resort to a Microsoft package which doesn't always work. - Not every tunnel is fond of keep-alive (Cloudflare), so I had to switch to ngrok (until I found out they have a limit of 20k requests).

I know this isn't the protocol's fault, and I'm sure there's something I'm missing, but my god is it frustrating.

Re: Server-Sent Events (SSE) Are Underrated

#4
So it’s websockets, only instead of the Web server needing to handle the protocol upgrade, you just piggyback on HTTP with an in-band protocol.

I’m not sure this makes sense in 2024. Pretty much every web server supports websockets at this point, and so do all of the browsers. You can easily impose the constraint on your code that communication through a websocket is mono-directional. And the capability to broadcast a message to all subscribers is going to be deceptively complex, no matter how you broadcast it.

Re: Server-Sent Events (SSE) Are Underrated

#6

So it’s websockets, only instead of the Web server needing to handle the protocol upgrade, you just piggyback on HTTP with an in-band protocol. I’m not sure this makes sense in 2024. Pretty much every web server supports websockets at this point, and so do all of the browsers. You can easily impose the constraint on your code that communication through a websocket is mono-directional. And the capability to broadcast…

[deleted]

Re: Server-Sent Events (SSE) Are Underrated

#7

So it’s websockets, only instead of the Web server needing to handle the protocol upgrade, you just piggyback on HTTP with an in-band protocol. I’m not sure this makes sense in 2024. Pretty much every web server supports websockets at this point, and so do all of the browsers. You can easily impose the constraint on your code that communication through a websocket is mono-directional. And the capability to broadcast…

Yes most servers support websockets. But unfortunately most proxies and firewalls do not, especially in big company networks. Suggesting my users to use SSEs for my database replication stream solved most of their problems. Also setting up a SSE endpoint is like 5 lines of code. WebSockets instead require much more and you also have to do things like pings etc to ensure that it automatically reconnects. SEEs with the JavaScript EventSource API have all you need build in:

https://rxdb.info/articles/websockets-sse-polling-webrtc-web...

Re: Server-Sent Events (SSE) Are Underrated

#8
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).”

Re: Server-Sent Events (SSE) Are Underrated

#9

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).”

And over HTTP/2 and 3 they are efficient?

Re: Server-Sent Events (SSE) Are Underrated

#10
> Perceived Limitations: The unidirectional nature might seem restrictive, though it's often sufficient for many use cases

For my use cases the main limitations of SSE are:

1. Text-only, so if you want to do binary you need to do something like base64

2. Browser connection limits for HTTP/1.1, ie you can only have ~6 connections per domain[0]

Connection limits aren't a problem as long as you use HTTP/2+.

Even so, I don't think I would reach for SSE these days. For less latency-sensitive and data-use sensitive applications, I would just use long polling.

For things that are more performance-sensitive, I would probably use fetch with ReadableStream body responses. On the server side I would prefix each message with a 32bit integer (or maybe a variable length int of some sort) that gives the size of the message. This is far more flexible (by allowing binary data), and has less overhead compared to SSE, which requires 7 bytes ("data:" + "\n\n") of overhead for each message.

[0]: https://stackoverflow.com/a/985704

Post reply on HN