Contents

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

GoodBad
Simple, works over plain HTTPWasteful: most requests return “nothing new”
Easy to scale, cache and debugDelay up to the polling interval
Works through any proxy or firewallMany 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 (ETag and If-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

Polling is fine for modest scale and slow-changing data. Move to push when you need low delay or have lots of clients.