Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How a chat app like WhatsApp works: persistent connections, message routing, offline queues, delivery receipts, group fan-out and end-to-end encryption.
Quick answer: A messaging app like WhatsApp keeps a persistent connection open between each phone and a chat server. When you send a message, your phone encrypts it and sends it over that connection. The server looks up which server the recipient is connected to and forwards the message. If the recipient is offline, the message waits in a queue and a push notification wakes their device. Acknowledgements flow back at each stage to produce the sent, delivered and read ticks. With end-to-end encryption, only the participants' devices hold the keys, so the servers relay messages they cannot read.
This article describes the general architecture of large chat systems, drawing on what WhatsApp and Signal have said publicly. The exact internals of any one company's system are not public and change over time.
HTTP's request-response model means the server cannot speak first. A chat app needs the server to push a message the instant it arrives.
So each client opens one long-lived, encrypted TCP connection to a gateway (or chat) server and keeps it open. WhatsApp uses its own compact protocol, originally derived from XMPP; web-based chat apps typically use WebSockets. The principle is identical: both sides can send at any time, and each message is a small frame instead of a full HTTP request.
Holding hundreds of millions of mostly idle connections is the first scaling challenge. WhatsApp is well known for building its backend on Erlang, a language designed for telecom switches, where each connection is handled by an extremely lightweight process. The company has publicly described handling millions of connections per server. Phones send periodic heartbeats so that dead connections are noticed and cleaned up.
When Alice sends "hello" to Bob:
Each hop is acknowledged, and unacknowledged messages are retried. Retries can cause duplicates, so the receiver deduplicates by message ID. Retry plus deduplication is how systems achieve "exactly once" behaviour in practice; see why distributed systems are hard.
If Bob has no active connection:
WhatsApp has said that messages are removed from its servers once delivered, and that undelivered messages are kept only for a limited time. The conversation history lives on the devices. That choice keeps server storage small and supports the privacy model. Other apps, such as Telegram's default chats or Slack, store history on the server instead, which makes multi-device sync easier.
Networks can reorder messages. Chat systems usually assign each message a sequence number or server timestamp per conversation, and clients display messages in that order. Clocks on different phones cannot be trusted for this; see why clocks can't be trusted in distributed systems.
A group message must reach every member. Conceptually, the server fans out the message, placing a copy in each member's delivery queue and handling online and offline members as above.
Encryption makes groups more interesting. Encrypting separately for every member would be expensive for large groups. The Signal protocol's approach, which WhatsApp has adopted, uses sender keys: each member generates a key for sending to the group and shares it with the other members over their one-to-one encrypted channels. A group message is then encrypted once and relayed to everyone.
Group size limits exist partly because fan-out cost grows with membership.
With end-to-end encryption, the server never has the keys. WhatsApp uses the Signal protocol, whose specifications are published in the Signal documentation. The main ideas:
The server still sees metadata: who is messaging whom, when, and roughly how much. Encryption protects content, not the fact of communication.
Photos and videos do not travel through the chat connection.
Large files therefore never clog the messaging path, and delivery of media can use CDNs.
"Online", "last seen" and "typing..." are ephemeral signals. They are sent only to people who currently have the relevant chat or contact visible, they are not stored, and it does not matter if one is lost. Treating them differently from messages keeps a very chatty feature cheap.
One grey tick: the server received your message. Two grey ticks: it reached the recipient's device. Two blue ticks: the recipient opened it (if read receipts are enabled).
With end-to-end encryption, message content is encrypted on your device with keys the server does not have. The service can still see metadata such as who you contact and when.
They wait, encrypted, on the server and are delivered when your phone reconnects. They are deleted from the server after delivery or after a retention period.
A persistent connection lets the server push messages instantly and avoids the overhead of repeated requests, which matters for battery life and latency.
A chat system is a relay built from a few robust ideas: persistent connections, a registry of who is connected where, queues for the offline, acknowledgements with retries, and encryption that keeps the relay blind. The scale is extraordinary, but the building blocks are the same ones you would use for a chat feature of any size.