Hearingabout it.
Keep asking, or leave a line open and let the server speak first.
Something happens on the server: an order lands, a build finishes, a message arrives. The browser can’t know until it hears. Short and long polling keep asking. Server-Sent Events and WebSockets hold a connection open, so the server can tell you itself.
This isn’t a simulation. Each lane is a real transport talking to the same Cloudflare Durable Object. An event fires every 5 to 20 seconds, and each lane races to notice. Open your browser’s network panel and watch them do it. Everyone on this page shares the same events.
- Events
- 0
- Last
- —
- Lanes
- Waiting
- Run
- 3:00 left
The clock also fires every 5–20 s, for everyone on this page.
- Request and answer
- Answer with news
- Held open
- Open connection
- Acknowledgement
- Event fired
Short polling
Asks every 2 s, news or not.
- Requests
- 0
- Wasted
- 0
- Avg delay
- —
Long polling
Asks once. The server holds the answer for up to 20 s.
- Requests
- 0
- Wasted
- 0
- Avg delay
- —
Server-Sent Events
One response that never ends. News flows down only.
Acks need a separate POST
- Requests
- 0
- Wasted
- 0
- Avg delay
- —
WebSocket
One open socket. Messages flow both ways.
Acks go up the same socket
- Requests
- 0
- Wasted
- 0
- Avg delay
- —
Scheduled batch
Once a minute, sends whatever the clock fired.
Cron, once a minute.
- Requests
- 0
- Empty runs
- 0
- Avg delay
- —
Waiting for the first event. The clock fires every 5 to 20 seconds, or fire one yourself.
How it's wired.
One clock, four lanes, one batch- Clock
One Durable Object keeps the clock for everyone. Its alarm fires an event every 5 to 20 seconds while someone is watching, and it goes to sleep 30 seconds after the last visitor leaves. Fire an event goes to the same object, so every lane hears about a press at the same instant.
- Short poll
A
fetchto/pollevery 2 seconds, carrying the number of the last event it saw. The hub answers at once, nearly always with an empty list. - Long poll
A
fetchto/long-pollthat the hub holds open. It answers the moment an event fires, or after 20 seconds with nothing, and the browser asks again straight away. The next request’s?after=doubles as the acknowledgement. - SSE
One
GETto/streamthat never finishes. Each event goes down it with its number as the SSEid, so a browser that reconnects picks up where it left off. Nothing can travel back up, so each acknowledgement is a separatePOST. - WebSocket
The upgrade goes straight to the hub, which keeps the socket with Cloudflare’s hibernation API: an idle socket costs nothing while it waits. Events go down it and acknowledgements come back up it.
- Cron
A Cron Trigger runs once a minute, asks the hub for everything fired since the last run, and sends it as one batch down the SSE stream. It runs whether anything happened or not.
- Timing
Your browser estimates the gap between its clock and the server’s from the fastest polling round trip, the way NTP does. Every delay is measured from the moment the server fired, and includes the trip to you.
#announce(event) {
// held requests answer…
for (const w of waiters) w.resolve();
// …and every socket gets a frame
for (const ws of ctx.getWebSockets())
ws.send(frame(event));
}while (running) {
const res = await fetch(`/long-poll?after=${cursor}`);
const { events, latestSeq } = await res.json();
events.forEach(notice);
cursor = latestSeq; // …and straight back in
}When I'd reach for each.
Direction, cost and what breaks| Transport | Direction | Held open | At scale | What breaks it | I reach for it when… |
|---|---|---|---|---|---|
| Short polling | Browser asks | Nothing | Requests grow with visitors × how often they ask, news or not. | Delay is the poll interval. Mobile batteries and rate limits suffer. | The news is slow, the audience is small, or I need it working by lunch. |
| Long polling | Browser asks, server answers late | One request per browser | One held request per browser, plus a new one after every event. | Proxies that time out quiet requests. Reconnect storms after a burst. | I need near-real-time through infrastructure I don’t control, or a fallback when streams are blocked. |
| Server-Sent Events | Server to browser | One response per browser | Plain HTTP, so it compresses and shares one HTTP/2 connection. | Buffering proxies. Six connections per site on HTTP/1.1. It can’t talk back. | The server does the talking: notifications, progress, live scores, AI tokens arriving. |
| WebSocket | Both ways | One socket per browser | State lives wherever each socket lands, and a deploy drops them all at once. | Proxies that block the upgrade. You build reconnects, heartbeats and backpressure yourself. | Both sides talk often: chat, multiplayer, shared editing. |
| Scheduled batch | Server, on its own schedule | Nothing | One run per interval, however many people are watching. | Delay is the interval, up to a minute here. Work piles up if a run fails. | Nobody is waiting: digests, reports, clean-up. If a minute is fine, don’t pay for a transport. |
The small print.
What the race doesn't show- Short polling’s waste is a setting, not the idea.
- Ask every 30 seconds and the waste drops, but the average delay climbs to 15 seconds. The interval trades requests for delay, and no setting gets you both.
- A Worker holds a request open cheaply.
- Cloudflare bills a Worker for CPU time, not for time spent waiting, so a held long poll or an open stream costs little here. A server with one thread per connection would tell a different story.
- Idle sockets aren’t free everywhere.
- This hub hibernates, so a quiet socket costs nothing. On an ordinary server every open socket holds memory, a load balancer has to keep it on one machine, and a deploy drops them all at once.
- Your network is kinder than most.
- Every lane here runs over the same connection. Mobile networks drop idle connections, corporate proxies buffer streams and block upgrades, and that is when long polling earns its keep.
- The delays are estimates.
- Your clock and the server’s disagree, so the rig estimates the gap from the fastest round trip. The numbers are good to about half a round trip, and every lane shares the same error.
- Cron is punctual to the minute, not the second.
- Cron Triggers run at most once a minute and can start a few seconds late. They also only fire in production, so the batch lane stays quiet on previews.