X-API-Key header. Use API keys and credential safety for creation, storage, and rotation.
1
Open the socket
2
Subscribe after open
3
Parse every message
4
Reconnect with backoff
Reconnect after close or network failure with capped backoff. Resubscribe only after the new socket opens.
Before you connect
Connect towss://api.0xarchive.io/ws. For server-side clients, prefer an Authorization: Bearer <api-key> handshake when the WebSocket library supports custom headers. The query-string API-key form remains a compatibility path for private server-side scripts; never expose it in browser code, logs, telemetry, or copied prompts.
Decide these before you open the socket.
Keep Alive
The application{"op":"ping"} command and {"type":"pong"} response can measure one client’s application round-trip time. Treat that as a client-side observation, not a published platform latency percentile.
Reconnect Policy
Reconnect with capped exponential backoff and jitter. Do not reconnect in a tight loop. After a new socket opens, resubscribe explicitly and rebuild any local state that depended on the previous stream. If the stream feeds a local book, replay window, alert, or model, treat the reconnect as a state transition that needs logging.Auth And Secrets
For server-side clients, prefer anAuthorization: Bearer <api-key> handshake when the WebSocket library supports custom headers. The query-string apiKey form remains a compatibility path for private server-side scripts. Load the key from the environment or application secret store, and never expose it in browser URLs, logs, telemetry, or copied prompts. Browser clients should connect through your backend.
Message Loop
Parse every message defensively. Expected classes include subscription acknowledgements, snapshots, updates, replay messages, pings or pongs, gap signals, and errors. Preserve any request or correlation identifiers that appear in the message so a streaming issue can be tied back to the connection session.Connection State Object
Use a local state object so reconnects and support logs describe the same stream.onopen, onmessage, onerror, and onclose. When the socket closes, clear old timers, move the status to reconnecting, open a new socket with backoff, then restore subscriptions from activeSubscriptions. If a gap arrives for a stateful channel, add the symbol to unsafeSymbols until the client rebuilds from a snapshot or replay checkpoint.