Real time: long polling, WebSockets and SSE

Letting the server speak first · Part of Architecture

Key idea

Practice

8 questions · about 4 min

Explained step by step

HTTP

The web was born on the client-server model: the client always starts, asking the server for data. So the server had no way to send something to the client on its own, without being asked. For a chat or a live scoreboard that's a problem: the client would have to ask every 1 second. It's like a waiter who can only talk to you if you call them: even if your dish is ready, they can't tell you.

Long polling

With long polling, the server doesn't close the connection when it gets the request: it answers only when there's a new message or a time limit (timeout) is reached, for example 30 seconds. As soon as the client gets the response, it sends a new request and waits again. That way it imitates the server pushing data. It's like staying on hold on the phone: you don't hang up and call back, you wait until someone answers.

Long polling is easy to implement, fits small projects and works almost everywhere. But it usually doesn't scale: it creates a new connection each time, which wears out the server; message order can fail with several requests; and it adds latency, because the server must wait for the next request to send the next thing. It's like a mail carrier who only delivers when you leave an empty envelope for the next letter.

WebSocket

WebSocket gives a full-duplex channel over 1 single TCP connection: a persistent connection where client and server can send data at any time, both at once. It has little overhead and is meant for true real time. It's like a phone call: once connected, both talk whenever they want, without hanging up and redialing for each sentence.

WebSocket has advantages: asynchronous messages in both directions, a good origin-based security model and it's lightweight for client and server. Disadvantages: if the connection drops, it doesn't recover on its own (you must reconnect yourself) and some old browsers don't support it, though that matters less and less. It's like a phone call: cheap and smooth while it lasts, but if it drops, you have to dial again.

Handshake

A WebSocket starts with a handshake: the client sends an HTTP request asking to switch to the WebSocket protocol (ws://). The server answers accepting, and from then on the connection stays open and both sides send data whenever they want. It closes when either side decides. It's like 2 people greeting each other before chatting: the greeting happens 1 time and then the talk flows.

Server-Sent Events

Server-Sent Events (SSE) opens a long-lived connection where the server pushes data to the client whenever it wants. It's 1-way: after the initial request, the client only receives; it can't send over that connection. It's easy to use, almost every browser supports it and it has no trouble with firewalls. But there's a limit on open connections and it doesn't support binary data. It's like a radio: you hear everything it broadcasts, but you can't talk back.

To choose: long polling is the simplest and fits small scale. WebSocket fits when both sides send a lot, like in a chat or a game. SSE fits when only the server sends, like a live scoreboard, stock prices or notifications. It's like choosing between asking every so often, a phone call or a radio: it depends on whether you need to talk or just listen.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Letting the server speak first