On an online auction's lot page, the live connection drops for twenty seconds near closing; how should the browser client detect it, reconnect, resubscribe and catch up, and what should the bidder see meanwhile?
answer
- silence is a signal too
- stale price is labelled, not hidden
- jittered delay, even after a deploy
- resubscribe what is on screen
- subscribe, buffer, read, then drain
basics
~20 sTreat missed heartbeats as a dead connection and label the price stale. Reconnect with capped, jittered backoff, resubscribe only the lots on screen, read each lot's current state with its sequence number, then apply buffered events newer than it.
solid answer
~40 sThe server sends a heartbeat every few seconds; if two intervals pass in silence, the client treats the connection as dead even if no close event fired, shows "reconnecting, price may be out of date" and greys the price. Bidding stays available because the bid is an HTTP request the server validates. It reconnects with **exponential backoff and jitter, capped**, so a whole audience dropped by a deploy does not return in one wave. On open it **resubscribes exactly the lots on screen**, buffers incoming events, does an **ordinary read** of each lot's state with its sequence number, renders that, then applies buffered events with a higher sequence. The countdown keeps running from the server's end time, flagged as possibly extended until the read confirms it.
code
pseudocode · 21 linesonSilenceLongerThan(2 * HEARTBEAT):
connection.close(); enterReconnecting()
enterReconnecting():
ui.markStale(onScreen.lots) // grey price, banner, countdown 'may extend'
attempt = 0
scheduleNext()
scheduleNext():
cap = min(MAX_DELAY, BASE * 2 ** attempt)
wait = max(serverHint or 0, random(0, cap)) // full jitter, even on attempt 0
after(wait, tryConnect)
tryConnect():
if not connect(): attempt += 1; scheduleNext(); return
for topic in onScreen.topics(): subscribe(topic, buffer=true)
for lot in onScreen.lots:
state = GET /lots/{lot}/state // price, top bids, endTime, seq
render(lot, state); lastSeq[lot] = state.seq
drainBuffered(lot, after=state.seq) // apply newer, drop older
ui.clearStale()go deeper
Recall that a live connection can drop without warning and that the screen must say when its data may be stale rather than freezing silently.
Explain heartbeat plus deadline, exponential backoff with jitter and a cap, and why the client resubscribes from what is on screen and then reads current state.
Show the ordering that cannot lose an event (subscribe and buffer, read with a sequence number, drain newer), keep bidding open over HTTP, and handle a deploy that drops every connection at once.
Weigh server replay against a catch-up read, set heartbeat and grace budgets against closing windows, and decide what the platform guarantees every live feature during a mass reconnect.
## What can go wrong, and why it matters here A **live connection** (a WebSocket or an event stream) can die in two ways: loudly, with a close or error event, or **silently**, when a network hop drops the connection and neither end hears about it for a long time. In an auction the silent case is the dangerous one: the page keeps showing a price that is no longer true, and the bidder may stop bidding because they think they are winning. The client's policy above the socket has four jobs: **detect**, **tell the user**, **reconnect without a stampede**, and **catch up** so the screen is true again. ## 1. Detect: a heartbeat and a deadline - The server sends an application-level **heartbeat** message every few seconds (say 10 s), so silence is meaningful. - The client keeps a **deadline**: if no message of any kind arrives for about two intervals, it closes the connection itself and enters the reconnecting state. A deadline is common practice for exactly this reason: relying only on the close event can leave a silently dead connection showing a live-looking price. - A loud close or error event short-circuits the deadline. ## 2. Tell the user: stale, not broken | Element | While reconnecting | |---|---| | Current price | greyed, labelled "may be out of date" | | Bid ladder | frozen, with a "reconnecting" banner above it | | Bid button | **stays enabled**: the bid is an HTTP POST, and the server rejects a stale amount with the current price | | Countdown | keeps running from the server's end time, with "closing time may have been extended" | | Watch count | hidden or greyed; an old approximation adds nothing | A short grace (a second or two) before showing the banner avoids flicker on a blip; past it, the user must not mistake a frozen screen for a quiet auction. ## 3. Reconnect: backoff with jitter, capped The client waits before each attempt, growing the delay exponentially up to a cap (for example 0.5 s, 1 s, 2 s, 4 s, up to 30 s) and choosing a **random delay within that range** (jitter). Jitter matters most when the drop is not personal: a **deploy that closes every connection at once** makes 50,000 browsers try again together, and without randomisation they keep arriving in synchronised waves. The client half of surviving that deploy is: - jitter even the **first** attempt after a server-initiated close; - honour a "reconnect after" hint if the server sends one before closing; - keep the catch-up read cheap: it is the same lot read the page loads with, so the server can answer many identical requests from a short shared cache. Coming back online or returning to the tab can trigger an early attempt, but `navigator.onLine` being true does not prove the server is reachable, so the attempt still runs through the same backoff. ## 4. Resubscribe and catch up Resubscription comes from the client's **registry of what is on screen now**: this lot, the user's own alert topic, any watchlist rows visible. It is not a replay of every subscription made since the page opened. Then the client closes the gap with an **ordinary read**, in an order that cannot lose an event: 1. Resubscribe, and **buffer** incoming events instead of painting them. 2. Fetch the lot's current state: price, top bids, end time, and the **sequence number** it reflects. 3. Render that state and record its sequence number as the last applied. 4. Apply buffered events whose sequence is higher, in order; drop the rest as already covered. Reading first and subscribing second leaves a window in which a bid can land after the read but before the subscription and never be shown. If the server can replay missed events from a sequence number, the client can ask for that instead, but the read is the path that always works, including when the gap is longer than the server keeps. ## What it costs The policy needs a server heartbeat, a per-lot sequence number on every event and in every read, and a registry of on-screen subscriptions. In exchange, the bidder always knows whether the price is trustworthy, a deploy does not become a self-inflicted outage, and the screen is known to be current once the banner clears.
- Should the bid button be disabled while reconnecting?No. The bid travels over HTTP and the server is the authority on the price, so a bid placed during a reconnect is either accepted or rejected with the current price, which the page then shows. Disabling it would cost the bidder the last seconds of a lot for a display problem.
- How long should the heartbeat interval be?Short enough that a dead connection is noticed well within a lot's closing window, long enough that the heartbeats are negligible next to the bids: a few seconds to around ten. The deadline is about two intervals, so one delayed heartbeat does not trigger a reconnect.
- What changes after a deploy drops every connection at once?The first attempt is also jittered, a server's reconnect-after hint is honoured, and the catch-up read is a shared, cacheable lot read so fifty thousand identical requests do not each hit the database. The banner appears after the same short grace as for a single drop.
- The close event will always tell the page that the connection has died.
- Reconnect immediately in a tight loop so the bidder misses as little as possible.
- After reconnecting, the WebSocket resumes where it left off, so no catch-up read is needed.
- Resubscribe to every lot the bidder has opened since the page loaded.
- Read the lot's state first, then subscribe; the order does not matter.
- Keep showing the last price as live while the connection is down.