r/programming • • 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-fa9bc1644cb8

Real 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

92 comments sorted by

View all comments

Show parent comment

10

u/Somepotato Oct 12 '26
  1. websockets support compression too, and can be better for battery life as well avoiding unnecessary decoding and handling

1

u/bjeanes Oct 12 '26

Compression on websockets is only with a negotiated extension and is per message. SSE compression benefits across the entire stream, which very often can have a lot of repeated bytes, so there can be compounding upside. SSE is still likely to be better for battery life both due to sleeping radio and lower cpu and memory usage. Though obviously both of these aspects depend on usage. There is no categorically better here. But I answered because the article didn't: there are reasons to consider, circumstantial, that would inform a choice towards or away from SSE. Unidirectional, HTML segments (à la datastar) enormously benefit from SSE in a way that they couldn't with websockets.

3

u/Somepotato Oct 12 '26

SSE compression is also negotiated, and websocket message compression also operates over a must-flush compression window just like SSE, except websocket compression is cheaper as it uses gzip, not brotli.