Keep devices connected and route to them
Every device holds one authenticated WebSocket, MQTT or custom TCP connection to a gateway. Gateways are connection holders, not chat owners. On connect they write (device_id -> gateway_id) into a registry with a short expiry, refreshed by heartbeats. To deliver an online message, the chat service stores it, looks up every target device and hands the envelope to the gateway that holds that device.
- Alice’s phone sends a frame to Gateway A.
- Gateway A passes it to the chat service.
- The chat service stores the envelope and asks the registry where Bob’s devices are connected.
- It hands the envelope to Gateway B, which pushes it down Bob’s open connection.
- If a device has no route, the service keeps the envelope and sends only a wake-up notification.
Diagram as text
Components: Alice’s phone (Client); Gateway A (API gateway); Chat service (Service); Message store (NoSQL DB); Connection registry (Cache); Gateway B (API gateway); Bob’s phone (Client). Connections: Alice’s phone → Gateway A (open connection); Gateway A → Chat service (send); Chat service → Message store (save first); Chat service → Connection registry (lookup devices); Chat service → Gateway B (deliver copy); Gateway B → Bob’s phone (push over open line).
Read:Learn: Networking fundamentalsLearn: Real-time delivery architectureLearn: Protocols and real-time transports
Check yourself
Why not have phones poll every few seconds?
Show answerHide answer
At this scale most polls are empty, and polling misses the half-second target. A persistent connection lets the server push when it has something.