skip to content

In a dating app's mobile chat, one message reaches the phone by push, by socket and by a history fetch; how does the client show it once?

level: seniorimportance: should knowfreq 57%

answer

  1. three doors, one room
  2. every path writes through one upsert
  3. the screen reads the store, not events
  4. count unread, never increment it
  5. push is a hint, not the record

basics

~20 s

Every path writes into the local database through one upsert keyed by message id, and the screen renders from that database. Copies merge into one row, counts are computed from stored rows, and a push copy never skips the gap check.

solid answer

~50 s

Treat push, socket and history fetch as three ways of learning about the same record. Each one calls a single function that upserts by message id — the sender's client id, which every copy carries — into the local database, and the chat screen and the unread badge are computed from that database, never from the arrival events. The sender's side has the same problem: the acknowledgement and the server's echo of their own message both carry the client id, so both merge into the local echo. A push copy is a hint: it can arrive late, out of order or not at all, and may carry only a preview, so it goes through the same gap rule and the history fetch supplies the authoritative copy. The delivered receipt is sent once per message id.

go deeper

for a junior

Remember that the same message can arrive more than once, and the fix is storing every copy under the message's id so it appears a single time.

for a middle

Describe the single upsert function, rendering from the database, and computing the unread badge instead of incrementing it per arrival.

for a senior

Show why push copies are hints under the same gap rule, how the sender's echo and acknowledgement merge, and how receipts and notifications avoid repeating per copy.

for a principal

Argue for one write path as a team rule: every new real-time source must enter through the store, which constrains feature teams but removes a whole class of duplicate bugs.

## Three doors into the same room A dating app's chat client learns about a new message in several ways, and in practice it often learns about the same message more than once: | Path | When it carries a message | Can it be late, repeated or missing? | |---|---|---| | **Data-only push** through the platform push service | The app is in the background or not running | All three: delivery is best effort, may be reordered, delayed or throttled, and a held push can be discarded | | **Live socket** | The app is in the foreground and connected | Repeated around reconnects; missing during a network switch | | **History fetch** | Opening a conversation, filling a gap, catching up after a reconnect | Overlaps whatever the other two already delivered | | **The sender's own echo** | The server sends the sender's message back to them, alongside the acknowledgement | A second copy of a row the sender already shows | Each copy carries the message's **client-generated id** — the id the sender's phone minted at compose time — and, once stored by the server, its **sequence number**. That id is what makes merging possible. ## One write path The rule is architectural rather than clever: 1. Every path hands what it received to **one function** that upserts into the local database, keyed by message id. 2. The upsert keeps the most complete version: a full body beats a truncated preview, a known sequence number beats none, a later state beats an earlier one. 3. The chat screen observes the database and redraws from it; no path draws a bubble directly. 4. Derived values — the unread badge, the last-message preview in the matches list — are **computed** from stored rows, never incremented per arrival. The badge is where duplicates show first. A design that adds one to the unread count whenever a message event arrives counts the same message once for the push, once for the socket and once for the history fetch. Computing "messages from the match with a sequence above my read marker" from the database cannot double count. ## Push copies are hints A data-only push is useful — it lets a backgrounded app store a new message so it is there when the user opens the chat — but nothing about it is guaranteed: - The platform may delay it, throttle it, or keep only the newest held one; on one platform, force-quitting the app discards a held one. - The push service may reorder pushes, so message 15 can arrive before 14. - A push payload is small — one platform's service caps it at 4 KB — so a long message may arrive only as a preview. So a push copy goes through the same upsert and the same **gap rule** as a socket copy: if it carries 15 while the client holds up to 12 with nothing missing, it is stored by id but the watermark stays at 12 until 13 and 14 are fetched. When the app next runs in the foreground it fetches after the watermark, and the authoritative copy replaces any preview. If the notification was displayed by the OS without the app's code running, the message reaches the database only on that later fetch — which is fine, because correctness never depended on the push. ## The visible side effects - **Notifications.** If the conversation is open on screen, the client does not raise a visible notification for a message the user is already reading. - **Delivered receipts.** The client sends a delivered receipt when a message id is first stored, not once per copy; repeats are harmless if the server ignores them, but there is no reason to send them. - **The sender's echo.** On the sending phone, the acknowledgement and the echoed copy both match the pending row by client id. If the acknowledgement was lost, the echoed copy can complete the job, supplying the sequence number and moving the bubble to sent. ## What goes wrong without it - Two identical bubbles after a reconnect, because the socket replayed what a push already stored. - A badge reading 3 for one message. - A sender's message shown twice: the local echo plus the server's copy appended as new. - A truncated push preview overwriting the full text already fetched. A strong answer names the single upsert keyed by id, the store-driven screen, computed counts, and push as a hint under the same gap rule.

  • A truncated push preview arrives after the full message was already fetched; what should the upsert do?
    Keep the full body. The merge rule prefers the more complete version of each field — full text over a preview, a known sequence over none, a later state over an earlier one — so arrival order never downgrades a row.
  • The user reads the conversation on a tablet; what should happen to the phone's notification?
    The read receipt, sent as 'read up to sequence N', reaches the phone through the server; the phone updates its read marker, recomputes the badge and removes notifications for messages at or below N. Whether that reaches a backgrounded phone promptly depends on push, so it is best effort.

  • Each delivery path can draw its own bubble as messages arrive.
  • Increment the unread count whenever a message event arrives.
  • A push copy can safely advance the conversation's watermark on its own.
  • The socket copy of my own message is a new message to append.
  • If the push arrived, the history fetch can skip that message.