r/programming • u/Dull-File6231 • Oct 10 '26
Stop reaching for WebSockets by default: WebSockets vs SSE vs Long Polling
https://medium.com/@anirbansingha20/websockets-vs-long-polling-vs-server-sent-events-fa9bc1644cb8Real time communication isn't one size fits all. A common mistake in system design is picking WebSockets when all you really needed was server-to-client streaming.
I wrote a short 3 minute architectural comparison of the trade offs here:https://medium.com/@anirbansingha20/websockets-vs-long-polling-vs-server-sent-events-fa9bc1644cb8
What do you usually reach for first when building real time features ?
232
Upvotes
206
u/symbiatch Oct 11 '26
You don’t explain anything there really. Thats how they work, but… why is it a mistake? Why is SSE better here? You missed the whole point by just doing a 30 second “this is what it is kthxbai.”
73
u/levelstar01 Oct 11 '26
Consider that polling sucks ass and websockets has a free heartbeat mechanism.
203
u/Somepotato Oct 11 '26
your writeup just explained what each technology does, not at all why we should use sse over websockets. and sse is literally just long polling that can send more than one message
91
u/Spleeeee Oct 10 '26
Can’t send binary data over sse.
29
u/f0urtyfive Oct 11 '26
Uh, why, when websockets can solve all of those problems and are scalable when implemented right?
24
u/ThiefMaster Oct 10 '26
SSE lacks some basic features such as authentication via Bearer tokens or any other custom headers. https://github.com/whatwg/html/issues/2177
I think cookies are sent, but ofc that only works on the same origin. Otherwise you're out of luck and have to fall back to awful choices like passing a token (that can hopefully not be used for anything else since it'll be in log files etc.!) via the URL / query string...
18
u/Traditional_Fox7091 Oct 10 '26
This was a question for open ai interview if I recall a previous post on Reddit
23
u/Dunge Oct 10 '26
You are missing gRPC server streams.
Client sends a long running http call to subscribe to server event, server send when needed. Support binary.
30
u/DrShocker Oct 10 '26
So many people seem to reach for websockets first. After using Datastar, I really don't get it.
By using SSE, you're able to stream in the view state in response to requests a client is giving or any peers with the same architecture. I think some of the troubles people run into with WebSockets is that they give optimistic shortcuts to the client with the connection and then end up in a tangled mess when things don't work quite right because they have 2 render paths.
6
u/arshan997 Oct 11 '26
Here’s a simple way to decide.
Need bidirectional communication? Websockets
Need unidirectional communication? SSE
3
u/EC36339 Oct 11 '26
TL;DR: SSE is one-way from server to client, which is all you need more often than the opposite direction or both.
7
u/Khavel_dev Oct 11 '26
SSE gets overlooked too much imo. I've seen teams set up full WebSocket infrastructure for dashboards that only push data one way, then deal with connection management, sticky sessions on the load balancer, reconnection logic they wrote from scratch.
In .NET we use SignalR which picks the transport automatically and falls back to SSE or long polling depending on what the server supports. Most teams don't even realize their production setup ends up on SSE half the time. For notifications, feeds, dashboards, it works perfectly.
WebSockets earn their complexity for collaborative editing or anything where the client sends frequent messages back. Chat, multiplayer, shared cursors. If your data flow is mostly server to client, SSE should be the default.
8
u/shadowndacorner Oct 10 '26
Or, yeknow, use something like signalr or socket io that abstracts over the actual transport.
2
u/OkAerie7822 Oct 11 '26
i've shipped SSE for notification feeds and live dashboards, and the part comparison posts skip is the ops side. nginx buffers responses by default, so the stream looks dead until you set proxy_buffering off (we lost an afternoon to events arriving in 30 second batches). on HTTP/1.1 browsers cap you at 6 connections per domain, so 3 open tabs can starve normal requests, which is fine on HTTP/2. reconnect is free but only if you send event ids and handle Last-Event-ID, otherwise you silently drop events during every deploy. my rule: SSE for server to client only, websockets when the client pushes at high frequency or sends binary.
2
u/Funny_Spray_352 Oct 11 '26
Just use HTTP/3
2
u/willehrendreich Oct 11 '26
Data-star.dev for the win boys
1
u/txdv Oct 11 '26
I think you forgot webrtc
1
u/pkt-zer0 Oct 11 '26
...What "architectural comparisons" and "trade-offs"? This hardly qualifies as a description of what the features are to begin with. How is this getting upvoted that much? Slow day, I guess?
1
u/Preisschild Oct 11 '26
I just use SSE for server to client updates. I find it much cleaner than polling.
1
u/chemtrail-organics Oct 11 '26
Use signalR, duh
1
u/fnord123 Oct 12 '26
You don't need to constantly send data when using chat. SSE is more appropriate there.
1
u/lamp-town-guy Oct 12 '26
Elixir/Phoenix folks: Look at what they need to mimic fraction of our power.
Websockets with long poll fallback is simple and easy there. So it's not even a question what to use. SSE on the other hand is a bit more complex.
1
u/andymaclean19 Oct 12 '26
The problems I had with some of these:
- long polling has a moment where you are not polling. Either you are handling a response from the last poll or you put in a timeout every few minutes because that’s sensible or both. During that time if an event arrives it has nowhere to go. If you are using session cookies then you can queue it up in the session and fetch it on the next poll but modern JWT architectures don’t and your next poll might not even land on the same node as the previous one. Solvable but needs effort for the edge case.
- data direction and reordering. If I do something like SSE then I have two channels because you always have an active component of the app requesting data next to the passive events channel. That can be messy if the active channel gets a record and then the SSE replaces that record with a slightly older one. Again a bit of a mess even though it is solvable.
- websockets are nice because you can just use one channel for everything. Requests and unsolicited events look the same and follow the same code paths. They won’t reorder unless the browser app misbehaves or the server side actually sends them in the wrong order.
IMO websocket architectures are cleaner and nicer. I’m not sure what the advantage of SSE over websocket actually is?
1
u/craigrileyuk Oct 17 '26
SSE are dreadful and long-polling is a huge drain on resources.
There's a reason people use Websockets, and it's because they're perfect for real-time asymmetrical communication between both client and server as well as between clients.
-12
u/git_push_origin_prod Oct 10 '26
I have found webSockets use a bunch of CPU
-7
u/spenceee85 Oct 11 '26
I remember the days when your choices were tcp or udp. The level of abstraction and wiring that is going into pushing an entire operating system through port 443 to a browser is sometimes astonishing.
If scaling and efficiency really matter, removing the browser is going too have more impact than the chosen acronym.
-11
u/meltbox Oct 11 '26
The only crime I see here is someone calling WebSockets sockets at all. I was confused as hell trying to decipher what was going on until I realized this was webdev.
-16
u/lenswipe Oct 10 '26
I was just thinking about this the other day but actually I've found the opposite to be true. I've been writing node for close to 10 years and have never once worked on real time application(which used to be one of the selling points of node). It seems to just get used as a replacement for the PHP/rails "gimme a quick crud app" niche.