Basic Ping Loop
Keep-Alive Model
Timer And Retry Guardrails
Keep-alive bugs usually come from stale timers. Give each socket instance its own ping interval, reconnect timer, and close handler. When a socket closes permanently or is replaced, clear those handles before creating the next socket. Otherwise the client can send duplicate pings, open multiple reconnect loops, or restore subscriptions twice.Reconnect Sequence
1
Stop old timers
Clear ping intervals and pending reconnect timers attached to the closed socket.
2
Wait with jitter
Use capped backoff so network churn does not become a reconnect storm.
3
Open a new socket
Authenticate the new connection through the same safe key path.
4
Restore subscriptions from tracked state
Recreate only the active channel-symbol pairs recorded by the client.
5
Rebuild unsafe state
For local books, replay, or alert state, use a fresh snapshot or replay checkpoint before trusting output again.
Production Review
A client that only handlesonopen and onmessage is a smoke test. A production stream consumer also handles onerror, onclose, keep-alive, replay stop conditions, gap events, unsubscribe behavior, and backpressure. Keep-alive behavior should be visible in logs so support can distinguish normal network churn from auth failure, rate pressure, or a broken consumer.