← Back to the map

Unit 1 · Lesson 6

Polling, long polling, WebSockets and SSE

HTTP only speaks when spoken to. So how does a chat app show Bob's message the instant he sends it, when your phone never asked for it?

By the end you can

Watch

Prefer reading? The same lesson is written out below.

Plain HTTP is request and response: the server can only answer, never start a conversation. For chat, live scores or notifications, the server needs a way to tell you something now. There are four common ways, from simplest to most capable.

open connectionopen connectionAliceChat serverBob
Two chat users each keep an open connection to a chat server. When one sends a message, the server pushes it down the other user's open connection straight away.

Polling

The client asks every few seconds: "Anything new?" Usually the answer is "no".

Fine for things that change slowly, like checking a report's status every 30 seconds.

Long polling

The client asks "anything new?" and the server holds the request open until there is something (or a timeout, say 30 seconds). As soon as the client gets an answer, it asks again.

Server-sent events (SSE)

The client opens one HTTP connection and the server keeps streaming events down it, one way: server to client. Browsers support it with a simple API and reconnect automatically.

Great for live feeds, notifications, progress bars and streaming AI responses. If the client needs to send something, it uses ordinary requests.

WebSockets

A WebSocket starts as an HTTP request, then upgrades into a long-lived, two-way connection. Either side can send messages at any time, with very little overhead.

The right tool for chat, multiplayer games and collaborative editing. The cost is on the server: every user holds an open connection, so a million online users means a million connections. Those servers are stateful (they hold your connection), which makes scaling and deploys harder: you need a way to find which server holds Bob's connection, and clients must reconnect when a server restarts.

Choosing

FeatureGood fit
Rare updates, simplicity mattersPolling
Server-to-client stream (feeds, notifications, progress)SSE
Two-way, low-latency (chat, games, co-editing)WebSockets
Real-time over infrastructure that blocks the othersLong polling as a fallback

Key idea: polling asks repeatedly; long polling waits for an answer; SSE streams one way from the server; WebSockets are a two-way open line. Open connections make servers stateful, which is the price of real-time.

Check yourself

An app polls every 5 seconds, and new messages arrive about once an hour. What's the main problem?

Your new flashcards

Practice them now. With a free account they come back in your daily review.

Card 1 of 3