Web & Networking › Real-Time Communication
Polling
Asking the server repeatedly whether something changed.
Also known as: short polling, long polling, polling vs push
Polling means asking the server repeatedly whether something has changed, on a timer, instead of the server telling you.
setInterval(async () => {
const res = await fetch("/api/jobs/42");
const job = await res.json();
if (job.status === "done") showResult(job);
}, 3000); // ask every 3 seconds
Use cases: waiting for a background job, refreshing a dashboard, checking for new messages.
Pros and cons
| Good | Bad |
|---|---|
| Simple, works over plain HTTP | Wasteful: most requests return “nothing new” |
| Easy to scale, cache and debug | Delay up to the polling interval |
| Works through any proxy or firewall | Many clients mean many requests, so server load adds up |
Making it better
- Pick a sensible interval, and back off when nothing changes (3 s, then 5 s, then 10 s).
- Stop when done, or when the tab is hidden (
document.visibilityState). - Use conditional requests (
ETagandIf-None-Match) so unchanged answers are tiny (HTTP caching). - Add jitter so thousands of clients don’t hit at the same instant (retry with backoff).
- Set an overall timeout, and show the user an error instead of polling forever.
- Respect rate limits (rate limiting).
Alternatives
- Long polling: the server holds the request open until something happens, then answers, and the client immediately asks again.
- Server-sent events: a one-way stream from the server.
- WebSockets: a two-way persistent connection.
- Webhooks: for server-to-server notifications (webhooks vs polling).
Polling is fine for modest scale and slow-changing data. Move to push when you need low delay or have lots of clients.