WebSockets and Real-Time Communication
On this page
Polling vs SSE vs WebSockets: choosing a real-time technique
Customers want to see their order move from Processing to Shipped without refreshing. Support wants live chat on the same page. Both are "real-time", but they need different things: the first only needs the server to push updates; the second needs messages flowing both ways.
Plain HTTP can't push: the client asks, the server answers. So you choose a technique.
| Technique | How it works | Direction | Good for |
|---|---|---|---|
| Polling | Client asks every N seconds | Client pulls | Rare changes, simple setups |
| Long polling | Request stays open until there's news, then repeats | Server push, emulated | Fallback when nothing else works |
| Server-Sent Events (SSE) | One HTTP response streams events | Server to client | Notifications, status updates, live feeds |
| WebSockets | Connection upgraded to a two-way channel | Both directions | Chat, collaboration, games |
Polling and long polling
Every client asks "anything new?" every few seconds. With 50,000 open order pages polling every 5 seconds, that's 10,000 requests per second, nearly all answering "no". And an update still waits up to 5 seconds to appear. Fine for a few hundred users; expensive at scale.
Server-Sent Events (SSE): one-way push
SSE keeps one ordinary HTTP response open and the server writes events into it as they happen:
GET /orders/42/events Accept: text/event-stream
event: status
data: {"status":"shipped","carrier":"DHL"}The browser's EventSource API reconnects automatically and resumes from the last event id. It works through most proxies because it's plain HTTP. For order status, SSE is the right size.
One limit to know: over HTTP/1.1, browsers allow only 6 connections per domain, shared across all tabs, and each SSE stream holds one open. A user with seven tabs open finds that the seventh hangs. Over HTTP/2, many streams share one connection and the limit is about 100 (MDN). Serve SSE over HTTP/2.
WebSockets: a two-way channel
A WebSocket starts as an HTTP request with an Upgrade: websocket header. After the server agrees, the same TCP connection becomes a two-way channel where either side sends messages any time, with very little overhead per message. Chat, typing indicators and collaborative editing need this. Slack and Discord both keep a persistent WebSocket per client for exactly these events.
Libraries handle the fallbacks for you. ASP.NET Core's SignalR, for example, uses WebSockets when possible and falls back to SSE or long polling.
Scaling WebSockets: connections are state
A REST request can land on any server. A WebSocket or SSE connection lives on one server for minutes or hours. That changes things:
- Fan-out across servers. User A is connected to server 1, user B to server 3. A message for B must reach server 3. Use a shared channel, such as Redis Pub/Sub or a message broker, so every server receives events and delivers them to its own connections.
- Load balancer settings. Idle timeouts must be longer than your heartbeat interval, or the balancer silently cuts connections.
- Deploys and reconnects. Restarting a server drops all its connections at once. Clients must reconnect with backoff and jitter (Day 74), or they'll stampede the remaining servers.
- Capacity. Plan by concurrent connections, not requests per second. Each open connection costs memory. Managed services such as Azure SignalR Service hold the connections for you, so your app servers only handle messages.
- Missed messages. A client that reconnects after 30 seconds missed everything sent meanwhile. Give each message a sequence number, and on reconnect, have the client ask for everything after the last one it saw. SSE has this built in through
Last-Event-ID; with WebSockets you design it yourself. - Authentication. Browsers can't set custom headers on a WebSocket connection, so the token travels in a cookie or the first message. The connection can also outlive the token. Check expiry on the server and close connections whose token has expired.
Check yourself
Server-Sent Events. Only the server needs to push updates, and SSE does exactly that over plain HTTP, works through most proxies, and reconnects automatically. WebSockets add two-way messaging, which this feature doesn't need, at the cost of a separate protocol and more infrastructure care.
A shared message channel between servers, such as Redis Pub/Sub, so each server receives every message and delivers it to the users connected to it.
Over HTTP/1.1, browsers allow 6 connections per domain across all tabs, and each SSE stream holds one. Serve it over HTTP/2, or share one stream between tabs.
When updates are infrequent, a delay of tens of seconds is acceptable, or the infrastructure can't hold long-lived connections. Polling is simple, works everywhere and is easy to cache, but wastes requests when nothing has changed.
Each connection is long-lived state on one server, so you can't freely move a user between servers. Deploys drop connections, load balancers must support long connections, and messages for a user connected elsewhere need a shared backplane such as Redis pub/sub.