Mr. Darto leaves his laundry at Mrs. Rini's Laundry and waits at the front. Eko works at the back, near the washing machines. Mrs. Rini sits at the front counter, facing the order board. At some point load #12, Mr. Darto's, moves on to ironing. Mrs. Rini can find out in two ways. The first way, pull (fetching the news): she walks to the back every 30 seconds and asks "anything changed?". The second way, push (sending the news): Eko calls out "Twelve, ironing!" the moment the load moves.
The web has the same problem. A site's data is kept on a server: a program, usually on another computer, that sends pages and data to browsers that ask. The server is like Eko, who knows first when something changes. The browser is like Mrs. Rini, who needs to get the news. Realtime on the web means a change on the server reaches the user's screen right away, without the user pressing refresh (reloading the page). The clearest example is a chat app like WhatsApp Web, where your friend's message appears by itself. Realtime on the web is almost always about push.
The project for this whole track: Mrs. Rini's laundry board
In the htmx track (htmx is a JavaScript library, ready-made code that works through HTML attributes), you built Mrs. Rini's order board at http://localhost:8000/board. The board is made with Laravel (a tool for building web apps in PHP) and reloads its own content every 30 seconds. The word localhost means your own computer. The board holds three orders:
| Order | Customer | Weight | Status | Cost ($7/kg) |
|---|---|---|---|---|
| #12 | Mr. Darto | 3 kg | Washing | $21 |
| #13 | Ms. Wulan | 2 kg | Ironing | $14 |
| #14 | Mr. Yoga | 5 kg | Ready for pickup | $35 |
A status always moves forward in the same sequence: Queued → Washing → Ironing → Ready for pickup. In this track you make the board realtime: Eko changes a status from a tablet near the washing machines, and Mrs. Rini's screen changes at that same moment. Later the customer's phone gets a "Ready for pickup" message. There is also a chat with the customer, a sign that shows who is online, a notes field that two people can type in together, and a dashboard. The realtime part runs on a small server at http://localhost:3000, next to the Laravel app. It is written with Node.js, a program that runs JavaScript outside the browser.
Realtime means "fast enough", not zero
Every change takes time to reach the screen. The delay between a change happening and it showing on the screen is called latency. Realtime means the latency is small enough for the feature. The largest latency a feature can accept is its latency budget. This table gives examples (ms means milliseconds, thousandths of a second):
| Feature | Latency budget | Why |
|---|---|---|
| Voice calls, games | under 150 ms | the telephone standard ITU-T G.114 advises a one-way voice delay of at most 150 ms; beyond that people start interrupting each other |
| Chat, the "typing..." sign | under 1 second | up to 1 second, a conversation still feels natural |
| The order board while a customer waits | a few seconds | the customer stands at the counter and asks "is mine done?" |
| A daily revenue chart | even 1 minute is fine | the numbers are read only occasionally |
There are also classic limits from Jakob Nielsen, a researcher of how people use websites: a delay under 0.1 seconds feels instant, under 1 second does not interrupt a person's thinking, and over 10 seconds loses people's attention.
Realtime on the web is soft realtime: when the news is late, users are annoyed, but nobody gets hurt. The opposite is hard realtime, for example a car's airbag: being even slightly late means it fails completely. So decide your feature's latency budget first, then choose the technology.
Pull: the browser keeps asking
The browser and the server talk through HTTP, the web's rules for sending messages. The pattern is always the same: the browser sends a request, then the server sends an answer (a response). The server cannot speak first. So to get the latest news, the browser has to ask again and again. This is called polling. Mrs. Rini's board does it through htmx, with the attribute hx-trigger="every 30s". The JavaScript version:
setInterval(async () => {
const res = await fetch('/board') // ask: the latest board?
document.querySelector('#board').innerHTML = await res.text() // paste the answer in
}, 30000) // every 30,000 ms = 30 seconds
setInterval(..., 30000)runs the function inside it every 30 seconds, without stopping. The wordasynclets that function useawait.fetch('/board')sends one request that asks for the board, andawaitwaits for the answer. The request is sent whether anything changed or not.- The line after it puts the server's answer into the
#boardelement on the page. - A change on the server does not show on the screen until the next request.
Before you look at the table below, guess. Eko changes the status of #12 at a random moment. On average, how long until the change shows on the board: 0 seconds, 15 seconds or 30 seconds?
Say the board asks at second 0, 30, 60 and so on:
| Eko changes the status at second | The board asks again at second | Mrs. Rini waits |
|---|---|---|
| 4 | 30 | 26 seconds |
| 29 | 30 | 1 second |
| 31 | 60 | 29 seconds |
The wait can be anything from almost 0 to 30 seconds, depending on when the change happens. When changes come at random moments, the average is half the interval (the time between two questions). So the answer is 15 seconds. The Lab on this page measures it: over 1,000 changes at random moments, the average wait is 14.8 seconds.
The cost of polling was measured too, in bytes (a unit of data size). The test used the Chromium 147 browser and a local server that copies this board, without cookies (small notes the browser sends with every request). One question takes 625 bytes for the request, 164 bytes for the response header (the information ahead of the content), and 234 bytes for the board itself. That is 1,023 bytes per question. There are 120 questions an hour, which makes 122,760 bytes per hour per tab, even when nothing changes at all. On the real Laravel app, the session cookie adds a few hundred more bytes to every request.
Push: the server tells you
Push needs a connection (a channel between the browser and the server) that is left open. The browser opens one connection to the realtime server, then waits. As soon as Eko changes a status, the server sends the news through that connection, and the board changes at once. The delay is only one network trip from the server to the screen: under 1 ms on localhost, and usually tens to hundreds of milliseconds on mobile internet.
Because the connection is already open, each piece of news is small. Two technologies that use an open connection are SSE (only the server sends) and WebSocket (both the browser and the server can send). The table compares them with polling for one hour with 6 status changes, measured in Chromium 147:
| Way | Requests per hour | Bytes per hour | Average wait |
|---|---|---|---|
| Polling every 30 seconds | 120 | 122,760 | 15 seconds |
| SSE (Server-Sent Events) | 1 | 819 + 6 × 65 = 1,209 | 1 network trip |
| WebSocket | 1 | 616 + 6 × 48 = 904 | 1 network trip |
The numbers 819 and 616 are the cost of opening the connection, paid only once. After that, one status update is only 65 bytes over SSE, or 48 bytes over WebSocket, because the HTTP headers are not sent again. The details are in the next lesson.
The map of options
The polling in this lesson is the first row of this table: short polling.
| Technology | Direction | How it works, briefly | Covered in |
|---|---|---|---|
| Short polling | the browser asks | a request every N seconds | this lesson |
| Long polling | the browser asks | the server holds the answer until there is news | Polling vs SSE vs WebSocket |
| SSE | server to browser | one HTTP response that never finishes | Server-Sent Events |
| WebSocket | both ways | one connection, messages back and forth at any time | WebSocket Fundamentals |
| Socket.IO | both ways | a library on top of WebSocket, plus reconnecting (connecting again when the connection drops) and rooms (groups of receivers) | Socket.IO Basics |
| WebRTC | browser to browser | voice, video and data straight between users | WebRTC Basics |
When polling is enough
Polling is not wrong. Use polling when:
- Half the interval still fits the latency budget. A revenue chart that the laundry owner checks occasionally may be 30 seconds late.
- The data rarely changes and few people watch it. One admin tab with 120 requests an hour is no problem.
- The infrastructure has to stay simple. Infrastructure means the servers and services that run your app. Polling only uses ordinary HTTP requests, so it works on any PHP hosting and through any proxy (a server in between). Every request is also independent of the others.
Mrs. Rini's board is different. A customer is standing at the counter and asking, so an average wait of 15 seconds feels long. Here push is the right choice.
⚠️ What people get wrong about realtime
- Thinking realtime means zero seconds. There is always a delay. Decide first how much delay your feature can accept.
- Shrinking the polling interval to get "realtime". Polling every 1 second means 3,600 requests per hour per tab, and the average wait is still half a second.
- Thinking the server can speak first over an ordinary request. The server can only answer requests. For push, the connection has to stay open, through SSE or WebSocket.
- Using WebSocket for everything right away. When data only flows from the server to the browser, SSE is simpler. When the data rarely changes and may be late, polling is enough.
TL;DR: Realtime on the web means data that changes on the server (the program that keeps a site's data) reaches the screen almost at once, without a refresh, like messages in WhatsApp Web. Realtime on the web is soft realtime: being late hurts nobody. First decide the latency budget, the longest delay your feature can accept (voice under 150 milliseconds, chat under 1 second, an order board a few seconds). Pull, also called polling, means the browser asks the server every N seconds. On average the news is half an interval late (15 seconds for polling every 30 seconds), and each question costs 1,023 bytes even when nothing changed: 122,760 bytes per hour per tab. Push means a connection (a channel) to the server is left open, and the server sends the news as soon as something changes. Each update is only 48 to 65 bytes and arrives within one network trip. The technology options: short polling, long polling, SSE, WebSocket, Socket.IO, WebRTC. Polling is still right when half the interval fits the latency budget, the data rarely changes, and the infrastructure (the servers that run the app) has to stay simple.
⚡ Realtime: it arrives without a refresh
Meaning: A change on the server (the program that keeps a site's data) reaches the screen right away, without the user pressing refresh, like messages in WhatsApp Web. The delay (latency) is never zero, so first decide the latency budget, the longest delay your feature can accept: chat under 1 second, an order board a few seconds.
⚡ Pull: the browser asks every 30 seconds
Write:
setInterval(async () => {
const res = await fetch('/board') // ask: the latest board?
document.querySelector('#board').innerHTML = await res.text() // paste the answer in
}, 30000) // every 30,000 ms = 30 seconds
Guess first: this code makes a laundry's order board ask the server every 30 seconds. Eko, who works at the laundry, changes the status of load #12 at a random moment. On average, how long until the change shows on the board: 0, 15 or 30 seconds?
15 seconds, which is half the interval (the time between two questions). The wait is anything from almost 0 to 30 seconds, depending on when the change happens. Each question still costs 1,023 bytes when nothing changed.
Meaning: fetch('/board') sends one request (a message that asks for something), and setInterval(..., 30000) repeats it every 30 seconds. Over an ordinary request, the server can only answer; it cannot send news first. So the browser asks again and again: this is called pull, or polling. A change on the server does not show on the screen until the next request.
⚡ Push: the connection is left open
Meaning: With push, the browser opens one connection (a channel) to the server, then waits. As soon as a status changes, the server sends the news through that connection, and it arrives within one network trip. Each update is only 48 to 65 bytes, because the HTTP headers (extra information sent with every ordinary request) are not sent again.
⚡ When polling is enough
Meaning: Polling is still right when half the interval fits the latency budget, the data rarely changes, and the infrastructure (the servers that run the app) has to stay simple. Shrinking the interval to 1 second does not solve the delay: that means 3,600 requests per hour per tab.