WebSockets vs Polling: Real-Time Updates in Your App
Polling asks the server over and over. WebSockets keep one line open. Here is the request math at 500 users and a plain rule for which to pick.
WebSockets vs polling is the choice you make whenever an app has to show something new without the user pressing refresh: a chat message, an order status, a live counter. Polling means the browser asks the server "anything new?" on a timer. A WebSocket means the browser and server hold one connection open and either side can send a message whenever it likes. For most small apps, polling every few seconds is fine and far simpler. WebSockets earn their complexity when updates must arrive within a second or arrive from both directions.
Your AI coding tool will pick one for you, often without telling you why. This article gives you enough to check that choice.
How polling works
The browser sends a normal request on a schedule, say every five seconds, asks for anything newer than the last thing it saw, and the server answers. Most answers say "nothing new".
// Ask for new orders every 5 seconds
setInterval(async () => {
const res = await fetch('/api/orders?since=' + lastSeen)
const orders = await res.json()
if (orders.length) render(orders)
}, 5000)Polling works on every host, through every proxy, with no special server support. If it breaks, you can see the requests in the browser's network tab. The cost is waste and delay: most requests return nothing, and a change can wait up to a full interval before anyone sees it.
How WebSockets work
MDN describes the WebSocket API as a way to open a two-way interactive communication session between the user's browser and a server, where you can send messages and receive responses without having to poll the server for a reply. The connection starts with an ordinary HTTP handshake and then stays open.
const socket = new WebSocket('wss://yourapp.com/live')
socket.onmessage = (event) => render(JSON.parse(event.data))
socket.onclose = () => scheduleReconnect()Updates arrive the moment the server has them, and the server can push without being asked. The cost is that the server must hold many connections open at once, and you must handle the connection dropping, which it will, on phones switching networks and laptops going to sleep.
The request arithmetic at 500 users
Take 500 people with your dashboard open, polling every 5 seconds. That is 500 divided by 5, or 100 requests every second, all day, whether or not anything changed. Over a minute it is 6,000 requests. If a typical change happens a handful of times an hour, nearly all of those requests are answered with an empty list.
Move the interval to 30 seconds and the load falls to about 17 requests per second, but a change can now take half a minute to appear. This is the real trade: polling lets you buy lower load with slower updates, and the right interval depends on how stale a user can tolerate.
With WebSockets the same 500 users hold 500 open connections and the server sends a message only when something changes. Load follows the number of changes, not the number of watchers. The price is that 500 simultaneous connections is a different thing to run than 100 short requests a second, and some hosting setups limit or do not support long-lived connections at all. Check your host's documentation before building on it, especially on serverless platforms, and if hosting choices are new to you, the guide to building an app with AI is the place to start.
The third option people skip: server-sent events
If updates only flow from the server to the browser, server-sent events are often a better fit than either. The browser opens one long HTTP connection and the server streams messages down it. It is simpler than a WebSocket, runs over normal HTTP, and browsers reconnect automatically. It cannot send messages the other way, so it suits live feeds, progress bars, and streamed AI responses, but not a chat box on its own.
A simple way to choose
Situation | Best fit | Why |
|---|---|---|
Order status or report that changes a few times an hour | Polling every 15 to 60 seconds | Cheapest to build and debug |
Dashboard where numbers move constantly, viewers only watch | Server-sent events | One-way push, simple to run |
Chat, multiplayer cursors, collaborative editing | WebSockets | Both sides send, low delay matters |
Progress of a long job the user started | Polling every 2 to 5 seconds, or server-sent events | Short-lived, ends when the job ends |
You do not control the hosting | Polling or a managed realtime service | Long-lived connections may not be allowed |
For the chat case specifically, see how to add real-time chat to an AI-built app. If the thing users want is an alert rather than a live screen, push notifications and a notification center solve a different problem and can replace a live connection entirely.
Things that go wrong with WebSockets
Messages arrive faster than the page can process them. MDN warns that the standard WebSocket interface has no backpressure, so a flood of messages can fill the device's memory, peg the CPU, or both. Send summaries or batches, not every tiny change.
Reconnection is missing. AI-generated code often opens the socket and forgets what happens when it closes. Ask for reconnection with a growing delay between tries.
Missed messages. If the connection drops for ten seconds, what happened in those ten seconds? Have the app fetch the current state after reconnecting.
Database connections. A live feature that reads the database for every viewer can exhaust it, which database connection limits in AI-built apps explains.
A prompt that makes the AI justify its choice
Before accepting a real-time feature, ask: "For this feature, how many people will be connected at once, how quickly must a change appear, and does the browser ever need to send data back? Choose polling, server-sent events, or WebSockets based on those answers, and tell me what happens when the connection drops." If the answer cannot name those three numbers, it is guessing. Anything that triggers background work on a schedule rather than a screen update belongs in a queue instead.
FAQ
Is polling bad?
No. It is the simplest approach and is perfectly adequate when updates are infrequent and a short delay is acceptable.
What is long polling?
The browser sends a request and the server holds it open until there is something to report or a timeout, then the browser immediately asks again. It reduces empty responses but is more awkward than server-sent events and is rarely the best choice for a new app.
Do WebSockets work on every host?
No. Support depends on the platform and sometimes the plan. Confirm in your host's documentation that long-lived connections are allowed and what the limits are.
Do I need WebSockets for streaming AI responses?
Usually not. Streamed text from an AI model normally travels one way, from server to browser, so server-sent events or a streaming HTTP response is enough.
How did this land?
About the author

Staff Engineer, Platform
Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.


