Real-time communication: polling, long polling, WebSockets, SSE
There are four ways for a server to get new data to a client quickly. The client keeps asking (polling), waits on an open request (long polling), keeps a two-way connection open (WebSockets), or receives a one-way stream (server-sent events, SSE).
Why it matters
Normal HTTP lets the client ask but not the server tell. Chat, notifications, live prices and multiplayer games need updates pushed to the user within a fraction of a second.
The idea
Normal HTTP is 'the client asks, the server answers'. Chat, live scores and notifications need the server to push. Polling is simple but wasteful. Long polling holds a request open until there's news. WebSockets keep one connection open for messages in both directions. SSE is a simpler one-way stream from server to client.
Polling is a child asking 'are we there yet?' every minute. Long polling is asking once, and the driver only answers when you arrive. A WebSocket is an open phone line both people can talk on.
An open phone line costs nothing to keep open. Each WebSocket holds memory and a connection on a server, so millions of users need many connection servers, and a way to find which server each user is connected to.
How it works
- Short polling: the client asks 'anything new?' every few seconds. Simple, but most answers are 'no'.
- Long polling: the client asks, and the server holds the request open until there's news (or a timeout of about 30 seconds). The client immediately asks again.
- WebSocket: starts as an HTTP request with an 'Upgrade' header. After that, the same connection stays open and either side can send a message at any time.
- SSE: the client opens one HTTP request and the server keeps sending events down it; the browser reconnects automatically if it drops.
- At scale: users connect to many gateway servers. When user A messages user B, the system looks up B's gateway in a registry (such as Redis) and forwards the message there.
Example
WhatsApp-style chat: each user keeps a connection to a gateway server, and a lookup store records which gateway holds each user, so a message can be routed to the right one.
Key points
- Short polling: simple, but most responses are empty; high load, and a delay of up to one polling interval.
- Long polling: fewer empty responses; works everywhere; one request per message.
- WebSockets: two-way and low latency, but connections are stateful, which makes load balancing and scaling harder.
- SSE: one-way, server to client, over normal HTTP; reconnects automatically; good for feeds and notifications.
- Stateful connections need a 'who is connected where' registry, often in Redis.
Common mistakes
- Choosing WebSockets for one-way updates, where SSE is simpler.
- Forgetting that open connections make servers stateful: you need a connection registry and reconnect logic.
- No plan for messages missed while a client was disconnected.
Not true: One-way updates (server to client): WebSockets are simpler than SSE.
In fact: For one-way updates, SSE is simpler: a one-way stream from the server to the client over ordinary HTTP. WebSockets earn their extra cost when both sides need to send.
Not true: Reconnecting: the client automatically receives the messages it missed.
In fact: Messages sent while a client was disconnected are lost unless the server kept them. Store them, and let the client ask for everything after the last one it saw.
In the interview
For chat I'd use WebSockets, because messages flow both ways with low latency. Users connect to a fleet of gateway servers, and Redis maps each user to their gateway so a message can be pushed to the right connection. For one-way updates like live scores, SSE is simpler.
Part of Unit 1: Networking and APIs in the free System Design course.