Chat that never loses a message
WhatsApp-shaped messaging: a connection registry finds the recipient's gateway, delivery is at-least-once with client-side dedup, and offline users fall back to push.
What this design does
Chat feels simple until you hold a million sockets: the sender's gateway isn't the recipient's, 'delivered' must survive crashes between the two, and phones vanish mid-conversation. This design persists before acking (a message the server said 'sent' about can never be lost), routes through a connection registry that knows which gateway holds which user, delivers at-least-once, and lets the client's message-id dedup turn that into exactly-once on screen. No socket? The same delivery worker falls back to push.
The patterns it composes
- Connection registry — userId → gateway lookup; sockets are location, not identity
- Persist-then-ack — the store write happens before the sender sees ✓ — crash-safe by order
- At-least-once + idempotent receiver — redelivery is allowed; the client dedupes by message id
- Presence — online/typing/last-seen from gateway heartbeats
- Graceful fallback (push) — no live socket → APNs/FCM carries the notification
Import it, attach your own numbers, and verify the design against failures — or read the engineering notes.