Video in progress

For a chat, order status notifications and a real-time analytics dashboard, which of WebSocket, SSE and long polling would you choose, and why?

WebSocket for chat, which needs fast two-way exchange. SSE is simpler for notifications and dashboards where data comes only from the server, and it fits the HTTP infrastructure well. Long polling only in environments where the others don't work.

📖 Answer

The choice is decided not by the technology but by the direction of traffic and by the infrastructure.

Chat. The client also writes often: a message, the "typing..." indicator, the read mark. You can make each of them a separate HTTP POST, but the latency and overhead are noticeable. WebSocket fits.

Order status. Events are rare and one-way. SSE is enough: plain HTTP, reconnection with Last-Event-ID is ready-made, proxies and CDNs understand it, and auth works with an ordinary cookie. Whatever the client needs to send to the server is plain REST.

Dashboard. One-way, but the stream can be dense. SSE plus conflation on the server (one aggregated update every 500 ms). There's no point in a browser chart updating 50 times a second.

The limits of SSE worth remembering:

  • on HTTP/1.1 the browser allows about 6 connections per domain. If a user opens 7 tabs, the new one hangs. HTTP/2 removes this problem, so serve SSE behind HTTP/2;
  • text only, no binary;
  • no custom headers in EventSource.

Today I see long polling only as a fallback option: a very strict corporate network or an old client.

On one project we started with SSE and moved to WebSocket when chat was added. That's a normal path: start with the simpler thing and add complexity when the need appears.