WebSockets, acks and delivering every chat message
Updated Nov 10, 2026 · 4 min read
A chat app has to do something ordinary web apps don’t. The server has to reach the phone the moment a message lands, without the phone asking first. On top of that, messages must arrive in order, exactly once on screen, and still turn up after the phone was off for a day. Each of those is its own small design.
Getting messages to the phone
HTTP is request and response, so the obvious approach is to ask the server for news every few seconds. That’s polling, and most of the answers are empty. Long polling improves on it. The server holds each request open until there’s something to say, answers, and the app immediately asks again.
A WebSocket goes further. It starts as an ordinary HTTP request with an Upgrade: websocket header, the server answers 101 Switching Protocols, and from then on the same TCP connection stays open with either side sending whenever it likes. That’s what chat apps use while the app is open.
| Approach | How it works | Delay | Cost |
|---|---|---|---|
| Polling | Ask every few seconds | Up to one interval | A request per phone per interval, mostly empty |
| Long polling | Server holds the request until there’s news | Low | A new request after every message |
| WebSocket | One open connection, both ways | Lowest | One connection per phone, kept open |
| Push notification | Apple or Google wakes the phone | Seconds, sometimes more | Best effort, can be dropped |
Push notifications through APNs and FCM are for when the app isn’t running. They’re not a delivery channel. Apple keeps only the most recent notification for a phone that’s switched off, so a push should say “you have news”, and the app fetches the real messages itself.
How many connections
An open connection that isn’t sending anything costs a little memory and almost no CPU, so one server can hold a lot of them. Plan for a number you’ve measured, say 50,000 per server, and size for the busiest moment rather than the daily total. If 1 in 10 of 50 million daily users is online at peak, that’s 5 million connections, or 100 servers. Keep some spare. When a server dies, all its phones reconnect at once and land on the others.
Keeping order
Two messages sent a moment apart can take different paths and arrive in the wrong order. Phone clocks can’t fix that, because they drift and people set them by hand. Instead, the server that stores a message gives it the next sequence number for that conversation. Every phone sorts by the number, so everyone sees the same order. A gap, like 41 followed by 43, tells the app a message is missing and it should fetch it.
Acks and ticks
Each tick a sender sees is an acknowledgement from a different place.
- Ana’s app sends the message over its connection.
- The server stores it with the next sequence number.
- The server acks Ana. One tick means the message is safe on the server.
- The server pushes it down Ben’s connection.
- Ben’s app acks it. Two ticks means it reached his phone.
- When Ben opens the chat, his app sends a read receipt.
If Ana’s app doesn’t get its ack, it sends again with the same message id, and the server ignores the repeat. That way a flaky network can cause a resend but never a duplicate on screen.
Offline delivery
Because every message is stored before anyone gets a tick, offline delivery is simple. Each app remembers the last sequence number it has for each conversation. On reconnect it asks for everything after those numbers and catches up in one go, whether it was offline for a minute or a week. Meanwhile a push notification nudges the phone to reconnect.
Presence
A phone that loses signal can’t say goodbye, so the server can’t rely on a clean disconnect. Instead each open app sends a tiny heartbeat, say every 30 seconds, and a user who misses two shows as offline. That’s steady load. Five million online phones at one heartbeat every 30 seconds is about 170,000 a second. Many apps only send presence updates to people who have that chat open, rather than to every contact.
Routing between servers
Ana and Ben are usually connected to different chat servers. Each server records who is connected to it in a shared registry, often Redis. To deliver, the server holding Ana looks up Ben and hands the message to his server, usually over a pub/sub channel for that server. If Ben isn’t in the registry, he’s offline, and he gets a push.
Groups work the same way. The server looks up every member, then sends one copy to each server that holds some of them, and each server delivers to its own connections. A message to a group of 300 spread over 40 servers is 40 hops, not 300.