What a chat system needs
A chat system delivers messages between users in near real time. Unlike a request response api, it must push messages to recipients the moment they arrive and keep them in sync across devices.
Persistent connections
- Clients hold a long lived connection, a websocket, to a connection server.
- A user is mapped to whichever server holds their socket, so the system can find where to deliver a message.
- A routing layer sends a new message to the server holding the recipient connection.
Storing and ordering
- Messages are persisted so they survive offline recipients and load on reconnect.
- Each conversation needs a consistent order, often by a sequence number or timestamp per chat, so all devices see the same sequence.
- Delivery and read receipts track message state.
Presence and offline
- Presence shows who is online, updated as connections open and close.
- If a recipient is offline, the message is stored and a push notification wakes their device, then they sync on reconnect.
Key idea
A chat system pushes messages over persistent websocket connections through a routing layer, persists and orders messages per conversation, and uses presence and push notifications for offline users.