skip to content

Real-Time & Push

Keeping an installed app current: a live transport per feature, a socket held through backgrounding, push delivery and chat send states. Interviewers probe what radios and OS limits do to it.

on this pageshow

explore

questions

21

In a mobile live trivia-show app, when should the client open and close its live socket as the player switches apps, and why?

level: juniorimportance: must knowfreq 52%

answer

  1. who keeps code running offscreen
  2. a frozen socket is not a live one
  3. connect in front, close behind
  4. a short grace for a quick app switch
  5. resync from the server, not a local timer

basics

~20 s

Open the socket when the app reaches the foreground and close it on backgrounding, perhaps after a short grace. The OS will not keep a socket alive for an unused app; push is the background path.

solid answer

~50 s

Tie the socket to the app being in front of the player. On foreground, connect, authenticate and resync the show's current state from the server; on background, close the socket cleanly, optionally after a short grace the app picks for a quick switch to another app. The reason is the OS: a backgrounded app is soon suspended and runs no code, and an idle device can cut network access as well, so a socket left open simply freezes. The server then sees a player who looks connected but answers nothing, and the client comes back to a socket it cannot trust. Anything the player must hear while away, such as 'the show starts in five minutes', goes through the platform push service. On return, never resume a local countdown: fetch the live question and its deadline from the server.

go deeper

for a junior

Recall the rule: socket open in the foreground, closed in the background, push for anything urgent while away, and why the OS forces it.

for a middle

Walk through the foreground steps in order: session check, connect, subscribe with the last applied event, fetch live state, then show the screen.

for a senior

Show why a frozen open socket is worse than a closed one for both sides, and why the countdown must come from the server's deadline after a resync.

for a principal

Frame the trade between reconnect cost and background liveness, and decide which show moments justify a push to a player who is away.

## What the OS does to a backgrounded app A **live socket** here is one long-lived connection (usually a WebSocket) over which the game server pushes questions, countdown ticks and results to the phone. The question is when the client should hold it. The answer starts with the operating system. A phone app has a small set of **app states**: in the **foreground** it is on screen and has the user's attention; in the **background** it is off screen; soon after that it is **suspended**, kept in memory but running no code; and at any time it may be **terminated** to reclaim memory. The two native platforms phrase it differently. One says a background app should do as little work as possible and suspends it shortly after it leaves the screen; the other, once the device sits idle, suspends network access for apps apart from short maintenance windows. Neither promises to keep an app's socket working for as long as the app wants. So a socket left open in the background does not stay *live*. It stays *open* on paper: the app cannot read from it, cannot answer heartbeats, and the server cannot tell the difference between a player who is thinking and a phone in a pocket. ## The policy: connect in front, close behind The design rule is to make the socket's lifetime follow the app's visibility. When the app comes to the **foreground**: 1. Check the session; if the access token expired while the app was away, refresh it first (the request pipeline owns that). 2. Open the socket and subscribe to the show, passing the last event the client applied so it can catch up. 3. Fetch or receive the show's current state: which question is live, the server's deadline for it, the player's score. 4. Show the live screen only once that state has arrived. When the app goes to the **background**: - **Close the socket cleanly**, so the server learns at once that this player left instead of waiting for a timeout. - **Optionally wait a short grace first**, a few seconds the app chooses (this scenario's assumption), so a player who flicks to a message and back does not pay a full reconnect. The OS may suspend the app before the grace ends, so the grace is a hint, never a guarantee. - **Stop heartbeats and timers** that would otherwise try to run on a frozen process. - **Hand anything urgent to push**: the server sends 'the final round starts in two minutes' through the platform push service, the path built for reaching an app that is not running. ## Comparing the options | Policy | What the player gets | What it costs | |---|---|---| | Leave the socket open in the background | Nothing once the app is suspended and cannot read it | Server holds a dead-looking connection; client returns to a socket it cannot trust | | Close at once on background | A reconnect on every app switch | One reconnect and resync each time | | Close after a short grace | Quick switches keep the socket | A few seconds of an idle socket; the OS may cut the grace short | | Close and rely on push while away | Alerts for the moments that matter | Push is best effort, so the app still resyncs on return | ## What the player sees, and the answer window A trivia show has hard deadlines: each question is open for a fixed window, say ten seconds (an assumption). If the player leaves the app mid-question, that window keeps running on the server, not on the phone. On return the client must not restart its own countdown from where it froze; it asks the server which question is live and when it closes, and draws the timer from that. A missed question is shown as missed. While the socket is coming up, the screen says so ('Reconnecting...') and answer buttons are disabled, rather than letting the player tap an answer that cannot be sent. ## Common mistakes - Assuming the OS keeps a socket for a fixed number of minutes in the background; the time is short, not promised and differs by platform. - Trusting the old socket on return because it still reports itself as open. - Resuming the local countdown instead of the server's deadline. - Holding the socket open 'just in case' instead of sending the few alerts that matter through push.

  • Why close the socket explicitly instead of letting the OS suspend the app and the server time it out?
    A clean close tells the server immediately that the player left, so it can free the connection, update the player count and stop sending to it. A timeout only fires after the server's own heartbeat deadline, during which it keeps sending into a frozen process and shows the player as present. The explicit close costs one message; the timeout costs server resources and wrong state.
  • A player opens the app forty minutes before the show and waits on the lobby screen; should the socket be open all that time?
    It may be, since the app is in front, but it need not be. The countdown to the start can be drawn from a start time fetched once, so the client can connect a few minutes before the show (an assumption) and save forty minutes of heartbeats. What it must not do is assume a socket opened earlier is still healthy when the show begins.
  • Should the grace period be long, say a minute, to avoid reconnects?
    No. The OS decides how long a backgrounded app keeps running, and it is short and not promised, so a long grace mostly means a socket the app can no longer service. Keep the grace to a few seconds for quick app switches and make reconnecting cheap instead.

  • The OS keeps a backgrounded app's socket alive as long as it is open.
  • A socket that still reports itself open on return can be trusted.
  • The local countdown can resume where it froze when the app returns.
  • A long background grace guarantees the socket survives a short app switch.
  • Push is unnecessary because the socket can deliver alerts in the background.
open as a page

In a dating app's mobile chat, why should the phone generate each message's id when the message is composed, before the first send attempt?

level: juniorimportance: must knowfreq 65%

basics

~20 s

An id minted once at compose time lets the client show the message at once, lets the server recognise a retried send as the same message, and lets every acknowledgement or returning copy update one row instead of adding another.

open as a page

In a parcel-tracking app, why can't a data-only push be trusted to keep the shipments list current, and what does the app do instead?

level: juniorimportance: must knowfreq 62%

basics

~20 s

A data-only push is best effort on APNs and FCM alike: it may be held while the phone sleeps, throttled, replaced or lost. Treat it as a hint, and fetch the shipments list whenever the app opens or returns to the foreground.

open as a page

Why can't a mobile auction app keep its bidding-room connection open while backgrounded, and what replaces it?

level: juniorimportance: must knowfreq 60%

basics

~20 s

To save battery the OS soon stops a backgrounded app from running or using the network, so its socket dies. Alerts go through the platform push service instead, and on return the app re-fetches the lot before trusting anything.

open as a page

In a dating app's mobile chat, what moves a message through sending, sent, delivered and read, and what does the sender see while offline?

level: middleimportance: must knowfreq 68%

basics

~20 s

Sending means only the phone holds it; sent, the server acknowledged it with a sequence number; delivered, the match's device stored it; read, they viewed it. Offline the bubble stays visibly pending, and failure shows with a retry, never a silent drop.

open as a page

In a parcel-tracking app, how should collapse identifiers and time-to-live be set so a 'driver is two stops away' alert never arrives hours late?

level: middleimportance: must knowfreq 55%

basics

~20 s

Give each alert a time-to-live as long as it stays true (minutes for 'driver nearby') and each shipment one collapse identifier, so newer news replaces older. The push carries an id and version; the app fetches the record.

open as a page

In a mobile auction app, which live transport would you choose for the bidding room, the watch list, the outbid alert and the seller's dashboard?

level: middleimportance: must knowfreq 62%

basics

~20 s

Choose per feature: a held stream only while the bidding room is on screen, refresh-on-open or a slow poll for the watch list and seller's dashboard, and the platform push service for outbid alerts when the app is closed.

open as a page

In a mobile live trivia-show app, the phone moves from station Wi-Fi to cellular and the socket looks open but no questions arrive; how should the client detect and recover?

level: seniorimportance: must knowfreq 50%

basics

~20 s

The connection was bound to the Wi-Fi address, so it died silently and no error arrives for a long time. Act on the OS network-change signal, a missed heartbeat reply or a missing game tick: open a new socket and resume.

open as a page

In a mobile live trivia-show app, how should the client resubscribe after a reconnect so it neither misses nor double-applies game events?

level: seniorimportance: must knowfreq 46%

basics

~20 s

Number every game event, keep the last sequence the client applied, and resubscribe with it so the server resends what came after. If that is too old, load a snapshot; drop events at or below the last applied number.

open as a page

In a dating app's mobile chat, the client holds messages up to sequence 10 when sequence 12 arrives over the socket; what should the client do?

level: seniorimportance: must knowfreq 60%

basics

~20 s

Store message 12 by id but keep the conversation's watermark at 10, the highest sequence held without a gap. After a brief hold, ask the server for everything after 10, merge by id, then advance the watermark.

open as a page

In a parcel-tracking app, how should the push device token be managed so that after one user signs out, the next person on the phone does not get their alerts?

level: seniorimportance: must knowfreq 52%

basics

~20 s

A device token addresses this app on this device, not a person; the server's token-to-account binding decides who gets alerts. Fetch the token each launch, register it with the account and an install id, and detach it at sign-out.

open as a page

In a mobile live trivia-show app, how do you choose the heartbeat interval on its live socket, weighing NAT idle timeouts against battery?

level: middleimportance: should knowfreq 42%

basics

~20 s

Set the interval comfortably below the shortest idle timeout assumed on the path, send a heartbeat only after a quiet spell, and give each reply a deadline. Shorter intervals detect death sooner but wake the radio more.

open as a page

In a mobile live trivia-show app, how should each client reconnect after its socket drops, including when the server drops thousands of players at once?

level: middleimportance: should knowfreq 47%

basics

~20 s

Try at once when the phone's own network just returned, otherwise back off exponentially with a cap and random jitter. After a server-wide drop, randomise even the first try, honour Retry-After, and reset the backoff only after a healthy spell.

open as a page

In a dating app's mobile chat, where should a just-sent message sit in the conversation before and after the server acknowledges it?

level: middleimportance: should knowfreq 58%

basics

~20 s

Until acknowledged, the local echo sits below every acknowledged message, in typed order. The acknowledgement gives it the server's sequence number, and from then on it sorts by that number like any other message, never by the phone's clock.

open as a page

In a dating app's mobile chat, how should the client send typing indicators and read receipts without flooding the connection or misleading the match?

level: middleimportance: should knowfreq 52%

basics

~20 s

Typing is ephemeral: send it at most every few seconds over the live socket only, never queued, and let the receiver expire it. Read receipts are durable but cumulative: one 'read up to sequence N', sent when messages are actually seen.

open as a page

In a parcel-tracking app, when should the app ask for notification permission, and what should it still do for a user who refuses?

level: middleimportance: should knowfreq 46%

basics

~20 s

Ask in context, just after the user tracks a first parcel and can see what alerts are for, not at first launch. If they refuse, show shipment news in the app when it opens, offer a settings path, and never nag.

open as a page

In a mobile auction app's bidding room, why might bids go as ordinary HTTP requests while prices arrive over a one-way server stream?

level: middleimportance: should knowfreq 48%

basics

~20 s

Bids are occasional and each needs a definite answer and a safe retry, which an HTTP request with an idempotency key provides; prices are a continuous server-to-phone flow that a one-way stream carries with built-in resume. Neither needs a socket.

open as a page

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%

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.

open as a page

In a parcel-tracking app sending high-priority data-only pushes for every list change, 'driver nearby' alerts start arriving late; why, and how should priorities be set?

level: seniorimportance: should knowfreq 42%

basics

~20 s

High priority is for pushes that end in something the user sees. One service demotes an app whose high-priority pushes rarely do, so its real alerts then wait while the phone sleeps. Send only visible, urgent alerts high.

open as a page

A mobile auction app's live prices never arrive for some bidders on hotel and office Wi-Fi; what transport fallback should the client take?

level: seniorimportance: should knowfreq 36%

basics

~20 s

Detect a refused socket upgrade or a stream whose first event misses a deadline, step down to long polling, which buffering proxies pass, resume from the last event id, and retry the better transport on the next network.

open as a page

Should a mobile auction app hold one live connection per feature, or share one across the bidding room, watch list and seller dashboard?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Share one connection, with per-topic subscriptions each screen adds and drops: the phone pays for one handshake, one keepalive schedule and one reconnect. The costs are a shared failure and one busy topic delaying another, which small messages and priorities contain.

open as a page