←All posts
WebSocketsPerformanceBrowser APIsReactJune 2025 · 5 min read

We Built a Real-Time Trading Platform. It Worked Great — Until Users Switched Tabs.

Here's the browser behavior nobody talks about, and how we fixed tab throttling, task queue dumping, and stale data in a live WebSocket trading dashboard.


Users would switch to another tab, come back to the platform, and the app would freeze for a few seconds. Charts stuttered. Live prices lagged. In a trading environment, that's not a UX annoyance — that's money.

What was actually happening?

Two browser behaviors were working against us simultaneously.

1
Tab Throttling — When a tab goes into the background, browsers deliberately throttle network requests and JavaScript execution to save resources. This delays WebSocket message processing — so while the user is away, your real-time data pipeline is silently choking.
2
Task Queue Dumping — While the tab is inactive, the browser queues up all deferred tasks. The moment the user returns? The browser dumps that entire backlog at once. Hundreds of queued tasks fire simultaneously — and the main thread gets crushed.

Result: the app freezes right when the user needs it most.

How we solved it

1
Page Visibility API — We used document.visibilitychange to detect tab switches. When the tab goes hidden, we pause non-critical processing. When it returns, we resume gracefully instead of letting the browser flood the thread.
2
WebSocket heartbeat management — We kept the socket connection alive but buffered incoming data during background state, then replayed only the latest snapshot on return — not the full backlog.
3
Task throttling on resume — Instead of processing all queued updates at once, we used a controlled queue with requestIdleCallback to spread the work across frames.
4
Stale data detection — On tab focus, we check timestamp deltas and trigger a fresh data fetch if the gap exceeds our threshold. No stale prices painted as live.
js
document.addEventListener("visibilitychange", () => {
  if (document.hidden) {
    pauseNonCriticalUpdates();
  } else {
    const delta = Date.now() - lastUpdateTimestamp;
    if (delta > STALE_THRESHOLD_MS) {
      fetchLatestSnapshot();
    } else {
      resumeFromBuffer();
    }
  }
});

Browsers are not passive containers. They actively manage your app's resources in the background — and if you don't account for that, your users pay the price at the worst possible moment.

The lesson

If you're building anything real-time — trading platforms, live dashboards, collaborative tools — the Page Visibility API isn't optional. It's essential.

💡

Pair visibilitychange with a WebSocket ping/pong heartbeat so you also detect silent disconnections that happen while the tab is hidden.

Written by

Mohd Shahid Iqbal

SDE3 Frontend

Follow on LinkedIn →