WebSocket or polling — and why AI streaming needs neither

The moment "real time" shows up in a project, someone says: "we'll need WebSockets for that."
Often you won't. And in the most common case of all — wanting a model's answer to type itself out letter by letter — you need neither WebSockets nor polling. There's a third option living there that nobody mentions.
Let's take all three honestly.
Polling: the client asks on a timer
Polling is the browser simply asking the server every N seconds: "anything new?"
setInterval(async () => {
const res = await fetch("/api/status")
render(await res.json())
}, 5000)
That's it. Ordinary HTTP requests, same as fetching an image or a page. Nothing special needed on the server, in the browser or at the network in between.
Its most underrated trait: polling is unkillable. A tunnel, an elevator, wifi dropping — the next request five seconds later just goes through, and the data catches up. Your code never even learns there was an outage.
WebSocket: a pipe open both ways
A WebSocket is one connection, established once and then kept alive. Messages fly across it in both directions at any moment: from the client and from the server, unprompted.
You pay for that in three ways.
The connection needs repairing. A browser WebSocket does not reconnect on its own — it drops and everything freezes until you write reconnect logic with a growing backoff. On mobile networks that's not an edge case, it's daily life.
Your hosting must hold connections. Plain serverless functions live for seconds and aren't built for a permanent pipe — you'll want a real server or a dedicated service.
State gets smeared around. While the connection is open, the server remembers who's on the other end. So scaling to several machines means deciding how they share subscribers.
The comparison that matters
| criterion | Polling | WebSocket | SSE | |---|---|---|---| | direction | client asks, server answers | both ways | server → client only | | latency | up to the poll interval | instant | instant | | on a dropped connection | self-healing | you write the reconnect | browser reconnects itself | | hosting | anything | needs a persistent server | almost anything, but holds a connection | | complexity | one line | noticeable | small | | reach for it when | statuses, rare updates | chat, cursors, games | streamed answers, progress, notifications |
Look closely at the dropped-connection row — in real life it decides more often than the latency row does.
The third option: SSE, the forgotten one
Server-Sent Events is the browser opening an ordinary HTTP request, and the server never closing the response — it keeps trickling data in as it appears.
const stream = new EventSource("/api/chat")
stream.onmessage = (e) => appendToken(e.data)
The traits that make it the right pick for most AI work:
- Reconnection is built into the browser. It drops,
EventSourcecomes back by itself, no code from you. - It's plain HTTP. No upgrades, and proxies and corporate networks don't trip over it.
- It only goes one way — honestly. The model's answer flows from server to you. Nothing needs to go back: the next question is a new request.
And here's the fact worth remembering: streaming from the major AI APIs runs on SSE. When you add "stream": true and see data: {...} lines, that's it. Which means streaming an answer is mostly passing that stream through to the browser, not building WebSocket infrastructure.
SSE has one limit, and it's an old one: over HTTP/1.1 a browser holds about six connections per domain. On HTTP/2, which is nearly everywhere now, the issue disappears.
Who should pick what
No fence-sitting.
Take polling when updates are rare and can wait: job status, "how many people are in the queue", a rate refreshed each minute, a dashboard. Nobody notices a five-second pause, and you add zero new moving parts. When in doubt, start here.
Take SSE when the flow goes from server outward and only that way: a model's answer typing out, progress on a long job, a notification feed. This is exactly the case where beginners wrongly reach for WebSockets.
Take WebSocket only when the client needs to send as often as the server does: a shared editor with other people's cursors, a multiplayer game, a voice room, a live chat with a "typing…" indicator. The tell is simple — both sides talk constantly. If only the server talks, WebSocket is overkill.
A practical rule for a first project: start with polling, move to SSE when the delay becomes noticeable, and reach for WebSocket only when you hit a genuine two-way need. The reverse order is the most expensive way to learn the same thing.
Won't polling every 5 seconds kill my server?
Usually not, if the response is cheap. Do the maths: a hundred users at a 5-second interval is 20 requests per second, which is nothing for any server. Trouble starts when each of those requests runs a heavy database query. Fixed with a couple of seconds of caching and a longer interval when the tab isn't focused.
Can I skip all of this and just refresh the page?
Often yes, and that's a legitimate answer. If the data changes once an hour, a "refresh" button is more honest than any real time. Real time costs money and bugs; switch it on when the user is genuinely sitting there waiting for the screen to change.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





