Stable WebSocket Market Data: Stop Random Disconnects in Your Algo
Broker WebSocket feeds dropping randomly? Learn why Indian ISPs kill long-lived connections and how a datacenter static IP plus correct reconnect logic fixes it.
The symptom
Your bot subscribes to KiteTicker, Fyers data socket or SmartWebSocketV2 at 9:15. Sometime between 10:30 and 14:00 the feed silently freezes or throws a 1006 close code. Candles gap, indicators go stale, and your strategy trades on old prices. Restarting fixes it — until tomorrow.
Why consumer connections kill WebSockets
- CGNAT table timeouts: your ISP's shared NAT silently drops "idle-looking" long-lived TCP flows.
- IP rotation mid-connection instantly orphans the socket.
- Wi-Fi power management and router firmware kill connections to save resources.
- Peak-hour congestion causes missed heartbeats; the broker closes what it thinks is a dead client.
Fix part 1 — move the connection onto datacenter networking
Route the WebSocket through your dedicated static IP. Datacenter NAT/routing is engineered for long-lived flows: no CGNAT eviction, no rotation, no power-save. This one change removes the majority of mystery disconnects. Most broker SDKs accept proxy parameters for their socket clients, or honour HTTPS_PROXY from the environment.
Fix part 2 — reconnect logic that assumes failure anyway
- Resubscribe your full instrument list after every reconnect — never assume server-side state survived.
- Track last-tick timestamp per instrument; alert if any symbol goes silent for N seconds during market hours.
- On reconnect, reconcile positions/orders via REST before trusting the stream again.
Heartbeats and keepalive
Enable TCP keepalive on the socket and respond to broker ping frames promptly (SDKs do this if you don't block their event loop). Never run heavy computation on the WebSocket callback thread — hand ticks to a queue and process elsewhere.
Result
Datacenter-grade path + defensive reconnects turns the ticker from your least reliable component into one you forget about. Combined with the same static IP carrying your order flow, both halves of your algo present one stable identity to the broker all day.