In a mobile live trivia-show app, when should the client open and close its live socket as the player switches apps, and why?
answer
- who keeps code running offscreen
- a frozen socket is not a live one
- connect in front, close behind
- a short grace for a quick app switch
- resync from the server, not a local timer
basics
~20 sOpen 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 sTie 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
Recall the rule: socket open in the foreground, closed in the background, push for anything urgent while away, and why the OS forces it.
Walk through the foreground steps in order: session check, connect, subscribe with the last applied event, fetch live state, then show the screen.
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.
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.