skip to content

When an online auction's watchlist page sits in a hidden browser tab for an hour, what should its live connection do, and what must happen when the user returns?

level: seniorimportance: should knowfreq 40%

answer

  1. hidden is not paused, it is unreliable
  2. a short grace, then let go
  3. close on pagehide, not unload
  4. what was on screen is not current
  5. one batched read on return

basics

~20 s

Keep the connection through a short grace period, then close it: hidden pages are throttled and may be frozen or discarded. On return, mark rows updating, reconnect, resubscribe the visible lots and read their state in one request.

solid answer

~50 s

When the tab goes hidden I start a **grace period** of about a minute, so a quick tab switch costs nothing, and drop high-rate subscriptions straight away while keeping the user's own alert topic. After the grace, I **close the connection**: a hidden page's timers are throttled, its animation frames stop in most browsers, it may be **frozen** (timers and fetch callbacks stop running) or **discarded**, and an open connection can keep the page out of the back/forward cache in some browsers, so I also close on `pagehide`. Alerts the bidder must not miss while away go through notifications outside the page, not this connection. On return, or on `pageshow` from the back/forward cache, rows show their last values marked "updating", the client reconnects, resubscribes only the visible lots and reads all their prices in **one batched request** before resuming live updates.

code

javascript · 18 lines
javascript
let graceTimer = null;

document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'hidden') {
    live.unsubscribeHighRate();               // keep only the user's alert topic
    graceTimer = setTimeout(() => live.close(), 60_000);
  } else {
    clearTimeout(graceTimer);
    resyncVisibleLots();                      // idempotent: mark updating, reconnect, one batched read
  }
});

window.addEventListener('pagehide', () => live.close());
document.addEventListener('freeze', () => live.close());

window.addEventListener('pageshow', (event) => {
  if (event.persisted) resyncVisibleLots();   // restored from the back/forward cache
});

go deeper

for a junior

Recall that a hidden tab is reported by the Page Visibility API and that browsers throttle timers and stop animation frames there.

for a middle

Explain the hidden-tab policy: grace period, dropping high-rate topics, closing the connection, and resyncing visible rows on return.

for a senior

Account for frozen and discarded pages, back/forward cache eligibility with pagehide and pageshow, and why absent-user alerts need a channel outside the page.

for a principal

Set the grace period and hidden-tab rules as a shared default across live features, trading server connection cost against reconnect cost and user expectations.

## What a hidden tab really is A tab is **hidden** when the user switches to another tab or minimises the window; the Page Visibility API reports it through `document.visibilityState` and the `visibilitychange` event. A hidden page is not simply paused, and not reliably running either: - **Timers are throttled.** Browsers apply budget-based throttling to timers in background tabs, so a "check every second" loop may run far less often. - **Animation frames stop.** Most browsers stop calling `requestAnimationFrame` callbacks in background tabs, so a page that paints on frames does not paint at all. - **The page may be frozen.** In the frozen state the browser suspends the page's freezable tasks, such as timers and fetch callbacks, until it is unfrozen, which may never happen. - **The page may be discarded.** Under resource pressure the browser unloads it while the tab itself usually stays visible in the tab strip; returning reloads it. So a hidden watchlist cannot promise it is current. Keeping its connection open for an hour pays for 40 lots of updates nobody sees, on a page that may not be processing them. ## The policy | Moment | What the page does | |---|---| | Becomes hidden | start a grace timer (about a minute); unsubscribe high-rate topics at once; keep the user's alert topic | | Grace expires | close the live connection | | `freeze` or `pagehide` | close the connection now, whatever the timer says | | Becomes visible again | mark rows "updating"; reconnect; resubscribe visible lots; one batched read | | `pageshow` with `persisted` true | the same as becoming visible: the page came back from the back/forward cache with its connection closed | ### Why a grace period Bidders flick between tabs. Tearing down and rebuilding the connection for a ten-second detour costs a reconnect and a read for nothing, so a short grace keeps the cheap case cheap. Dropping the high-rate topics immediately still sheds most of the traffic. ### Why close on `pagehide`, not `unload` The back/forward cache keeps a page in memory so Back is instant. An open connection can make a page ineligible for it in some browsers, and lifecycle guidance is to close open connections in `pagehide` or `freeze`. An `unload` listener is the wrong place: it is unreliable and can itself block the cache. ### What about alerts while away "You've been outbid" matters most when the bidder is not looking. A connection in a hidden, possibly frozen tab is not a dependable way to deliver it, and a closed tab has no connection at all. The dependable path is a notification sent outside the page; the live connection's job is the page the user is looking at. ## Choosing the grace period - **Too short** and a bidder who glances at another tab pays a reconnect and a read every time, multiplied across thousands of tab switches at closing time. - **Too long** and the server holds connections for people who have gone to lunch. - **A minute is a starting default,** to be tuned from how long users are actually away before returning, which the page can measure for itself. - **High-rate topics never get a grace period;** only the low-volume alert topic stays subscribed through it. ## Coming back The return path reuses the reconnect logic, in this order: 1. **Show the truth about staleness.** Rows keep their last price, greyed with "updating", rather than appearing current or blanking out. 2. **Reconnect promptly.** The user is waiting, so the first attempt goes out at once; returning tabs are spread out by human behaviour, unlike a deploy. 3. **Resubscribe only what is visible.** Rows scrolled off screen do not need live prices yet. 4. **Read once, not forty times.** One batched request returns current price, end time and sequence number for every visible lot; each row renders from it, then buffered live events newer than its sequence apply. 5. **Clear the "updating" marks** row by row as each one is confirmed. ## What it costs The client needs a visibility handler, a grace timer, lifecycle handlers for `pagehide`, `freeze` and `pageshow`, and a batched read endpoint for the watchlist. In return the server holds connections only for pages someone is looking at, the page stays eligible for an instant Back, and a returning bidder sees prices that are labelled honestly until they are confirmed.

  • The user returns after the tab was discarded. What happens?
    The page reloads from scratch, so it loads the watchlist through its normal reads and opens a fresh connection. Nothing in memory survived, which is why anything worth keeping, such as scroll position or a half-typed maximum bid, should have been saved to session storage while the page was still hidden.
  • Why not just slow updates down while hidden instead of closing?
    Slowing helps the server only if the client asks for fewer events, and it still holds a connection per hidden tab. Unsubscribing high-rate topics at once already sheds most traffic; past the grace, closing frees the connection, keeps back/forward cache eligibility, and the return path resyncs anyway.

  • A hidden tab keeps running its timers and frames normally, so nothing needs to change.
  • Keep streaming while hidden so the watchlist is already current when the user returns.
  • Close the connection in an unload handler.
  • On return, trust the prices on screen because the connection was open.
  • Rely on the hidden page's live connection to deliver outbid alerts to an absent bidder.
  • On return, fetch each of the forty lots with its own request.