Video in progress

For a mobile app, should real-time updates go over a persistent WebSocket, or be combined with push notifications (FCM/APNs)?

The mobile OS cuts a background app's connections quickly, so WebSocket works only while the app is open. You need a hybrid model: FCM/APNs for delivery in the background, WebSocket while open, and both relying on one inbox and seq.

📖 Answer

iOS stops an app's network connections a few seconds after it goes to the background. On Android, Doze mode does the same. So the plan "keep the WebSocket open in the background too" doesn't work; and even if it did, it would eat the battery and may violate store rules.

The hybrid model:

  1. App open: WebSocket. Fast, two-way, with transient signals like "typing...".
  2. App in the background or closed: the delivery service sees that the user isn't online (presence or the lack of an ack) and sends a push notification.
  3. The app is reopened: the WebSocket connects and resumes from the last seq. What came through the push notification is in the same inbox, so a duplicate is dropped by seq.

Delicate points:

  • Notifying twice. The user sees the same message both as a push and inside the app. Wait briefly before sending a push (say 5 to 10 s), and send the push only if no WebSocket ack arrives in that time.
  • Push is not reliable. FCM/APNs don't guarantee delivery and may coalesce or delay messages. Push is a signal, not a source of data.
  • Payload. Don't put confidential data in a push: it passes through Google/Apple servers and shows on the lock screen. "You have a new message" and an ID are enough.
  • Silent push is used to refresh data in the background, but the OS throttles it: if you send it too often, it stops arriving.

In effect this makes WebSocket a "real-time optimization", while the inbox is the source of truth. That simplifies the system a lot: if the connection dropped or the push didn't arrive, the client gets the full state anyway the next time it opens.