Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Error Handling & Reconnection

Handshake errors

HTTP codeCauseAction
426Malformed upgrade (missing/invalid Sec-WebSocket-Key)Fix the WebSocket client
401Missing X-API-Key headerAdd the header
403Invalid, revoked, or expired keyRequest a new key
429Rate or connection limit hitBack off, see Rate Limits

Close codes

CodeReasonMeaningReconnect?
1000key_expiredKey expiration date passedNo — get a new key
1000key_invalidatedAdministrator revoked the keyNo — get a new key
1008too_slowClient lagged > 10 messages behindYes — process faster
1008rate_limit_exceededSent > 3 messages / minute to the serverYes — stop sending
1009frame_too_largeSent a frame larger than 1 KBYes — fix the client

Reconnection strategy

Exponential backoff, capped at 5 minutes:

attempt 1 → 1 s
attempt 2 → 2 s
attempt 3 → 4 s
attempt 4 → 8 s
…           cap at 300 s

Reset the counter after a successful connection.

Decision logic:

TriggerAction
Close key_expired / key_invalidatedStop
Close rate_limit_exceededWait 60 s, stop sending client messages, reconnect
Close too_slowReconnect immediately, process faster
HTTP 429Back off (you hit a connection limit)
Any other disconnectReconnect with exponential backoff

Detecting dead connections

The server sends a WebSocket PING every 15 s and closes the connection if no PONG arrives within 30 s. Your library handles PONG automatically and surfaces the disconnect as a close event.

import asyncio, json, websockets

async def stream(ws):
    try:
        async for raw in ws:
            data = json.loads(raw)
            if data["type"] == "announcement":
                handle(data)
    except websockets.ConnectionClosed as e:
        print(f"closed code={e.code} reason={e.reason!r}")

Don’t wrap recv() in a timeout shorter than ~60 s as a liveness check. Listings are sparse — you’ll time out and reconnect during quiet periods. Trust the library’s PING/PONG, or watch for the 30 s heartbeat.