Sub topic 12
Notifying offline users
This chapter assumes you have read the chapters on the one-to-one message flow and on presence. You should know that a chat server pushes messages over a persistent connection, and that presence comes from heartbeats with a time-to-live (TTL).
The problem
A message can only travel over a WebSocket if the recipient has one open. When the recipient is offline, the connection is gone. The message is still stored, but the user has no way to learn about it. A push notification fixes this. It goes through the mobile platform's own channel, which works even when your app is closed.
Choosing when to push
The delivery flow gives the server a simple decision at the point where it routes a message:
- Store the message and assign its sequence number.
- Look up the recipient's connections.
- If a connection exists, deliver over it and wait for an ack.
- If none exists, or no ack arrives in time, send a push notification.
Presence feeds step 2, but treat it as a hint. A heartbeat TTL means a user can look online for up to 30 seconds after the connection dropped. If you trust presence alone, you miss messages in that window. Use the missing ack as the final trigger.
The push path
Apple and Google run the push services, called APNs and FCM. Your server does not talk to the device directly. The flow is:
- At install time, the app asks the OS for a device token and sends it to your server.
- The server stores the token with the user's device record.
- To notify, the server sends the token and a payload to APNs or FCM.
- The platform service delivers the notification to the device.
Delivery is best effort. The platform may delay, collapse, or drop notifications, for example when the device is off for a long time. Never treat a push as the message itself.
Practical details
- Multiple devices. A user may have a phone, a tablet, and a laptop. Send a push to each device that has no live connection.
- Invalid tokens. APNs and FCM report tokens that are no longer valid. Delete them, or you waste calls on dead devices.
- Muted chats. Check the user's mute settings before sending.
- Groups. Pushing to every offline member of a large group can create a burst of requests. Send through a queue and let a pool of workers call the platform services.
- Collapsing. Use a collapse key so ten messages from one chat produce one notification with a count, not ten alerts.
- Badge counts. Compute unread counts from stored sequence numbers, not from the number of pushes sent.
Decoupling with a queue
Do not call APNs or FCM inside the message-send request. Those calls are slow and can fail. Put a notification job on a queue after the message is stored. A separate worker reads the job, builds the payload, and handles retries with backoff. A push failure then never blocks or loses the chat message.
Exercise
A user's phone lost signal 10 seconds ago, and the heartbeat TTL is 30 seconds. A friend sends a message. Presence still says online, and the WebSocket write goes nowhere. How does the system still notify the user, and why should the push carry only a short signal?
Show answer
The server waits for an ack over the connection. None arrives, so it falls back to a push notification through APNs or FCM. The push is best effort and can be delayed or dropped, and it passes through a third party. So it carries only a small signal, and the app fetches the stored message by sequence number once it reconnects.