How We Stream an Option Chain Without Drowning the Browser
Subscribing to market data only while it's on screen, and letting go the moment it isn't: how an option chain stays fast with ticks written to refs, batched into the store, and every token owned by one screen.
An option chain is the heaviest screen in a trading app. The rule we follow to keep it light is simple: subscribe only while the data is on screen, and unsubscribe as soon as it isn't.
The problem
For one underlying, such as an index, a single expiry can have 50 to 100 strikes. Each strike has a call and a put, and each side needs its own live feed: price, open interest, and the Greeks (delta, gamma, theta, vega, rho and IV). That is around 100 to 200 instruments updating several times a second.
If you subscribe to everything the moment the app opens, you pay for it everywhere.
- The socket carries more data.
- JavaScript parses more messages.
- React re-renders more often.
- The phone battery drains faster.
- And most of the time, the user isn't even looking at the option chain.
1. Subscribe only when the tab is active
The stock page has several tabs: Overview, Option Chain, Futures, Technicals and others. All the tabs are mounted, but only one is visible. The option chain keeps an isActive flag and behaves like this:
- Tab opens: subscribe to the Greek feed for every strike in the selected expiry.
- Tab closes: unsubscribe from all of them and clear the stored values.
- A user on the Overview tab costs the app nothing for option data.
// Tab opened: start streaming
useEffect(() => {
if (isActive) subscribeToStrikes(optionChain, true);
}, [isActive]);
// Cleanup runs when the user leaves the tab
return () => {
clearInterval(intervalId);
subscribeToStrikes(optionChainData, false); // stop the feed
dispatch(clearOptionChainStores()); // free memory
};2. Guard against late responses
The chain is loaded through an API call, and the subscription happens when it returns. A user can leave the tab before the response arrives, and the late response would then subscribe to a feed nobody is watching.
So the subscribe function checks first:
function subscribeToStrikes(chain, subscribe) {
if (subscribe && !isActiveRef.current) return; // user already left
...
}- It reads a ref, not state, so it sees the current value even inside an old closure.
- Unsubscribing is always allowed.
- Subscribing is allowed only while the tab is open.
3. Only the expiry you're looking at
An underlying can have many expiries: weekly, monthly and further out. We subscribe only to the strikes of the selected expiry. When the user switches, the old expiry is unsubscribed and the new one is subscribed.
useEffect(() => {
if (prevExpiry !== expiry) subscribeToStrikes(chain, false, prevExpiry); // stop old
subscribeToStrikes(chain, true); // start new
}, [expiry]);At any moment, the live subscriptions match the screen, whichever expiry that is.
4. A limited window of strikes
We don't fetch every strike. The chain request asks for a window around the current price (the num parameter), and we centre it on the live price of the underlying.
- If the underlying's price isn't in memory yet, we fetch the last traded price first, then request the chain.
- That is an extra call, but it avoids loading a chain centred on the wrong strike.
- Fewer strikes means fewer tokens to subscribe to in the first place.
5. Tag every subscription with its owner
Many screens can want the same token. The option chain, a watchlist and an order window might all show the same call option.
- Every subscription carries a location tag, for example the option chain's own id.
- Unsubscribing removes only that tag.
- The feed stops when no owner is left.
- The option chain closing never cuts another screen's price.
6. Don't let every tick re-render React
The Greek feed is the busiest in the app. If every message updated React state, the table would re-render continuously. Instead, the data takes three steps:
The user sees prices change fast enough, and the main thread stays responsive. The timer exists only while the tab is active.
7. Clean up completely
When the tab closes, we do more than unsubscribe. We empty the stored values for LTP, OI and every Greek.
- Stale numbers don't flash when the user returns.
- The memory held by hundreds of entries is released.
What this buys you
- Less network and CPU: with no option chain open, there are no option feeds to receive or parse.
- Fewer renders: the batched 500 ms update replaces per-tick renders.
- No leaks: each subscription has one owner, one cleanup and a guard against late responses.
- A better phone experience: less work in the background means less battery use and heat.
I haven't published measured figures for this yet, such as messages per second with the tab open and closed, or scripting time in the Performance panel. I'll add them once I've measured them.
The checklist
The cheapest tick is the one you never subscribed to.
Written by
Mohd Shahid Iqbal
SDE3 Frontend