Skip to main content

Client

Session

start — builds the subscription from registered handlers and dispatches notifications. Blocks until stop() is called or a fatal error occurs.
Only one start() per instance — a second call raises RuntimeError. stop — signals the dispatch loop to exit, closes the session, and resets state.

Handlers

Register event handlers with decorators. Multiple handlers on the same event type run sequentially in registration order.
Three decorator forms are supported:
on_trace_invalidated — fires when a previously delivered pending or confirmed trace becomes invalid. Discard cached data for that trace.
An exception inside a handler halts dispatch for that notification (fail-fast).

Filtering

The streaming API delivers all transactions for the subscribed addresses. To filter by specific criteria (opcode, amount, source, etc.), apply the logic inside the handler:
Opcodes are hex strings (e.g. "0x7362d09c"). See Action Types for the full ActionType enum.

Finality

Every trace-based notification carries a finality field. The lifecycle is monotonic per trace: pendingconfirmedfinalized. At any point before finalized, a trace_invalidated event may signal rollback. min_finality sets the minimum level a handler accepts. Default: Finality.FINALIZED. Convenience properties: .is_pending, .is_confirmed, .is_finalized.

Connection State

Properties: state, is_subscribed, is_connecting, is_reconnecting.
Monitor transitions via on_state_change callback (sync or async):

Reconnection

Automatic reconnection on transient failures (5xx, 429, streaming transport errors, heartbeat timeout). All other client errors (400, 401, 403, 404, 405, 409, 422) are fatal and stop immediately.
Always call stop() in a finally block. Without a clean disconnect the server may hold the session open, blocking new connections until it times out.

Key Rotation

Pass a list of key strings to rotate on connection limit errors. No rate limiters — streaming is connection-based.
If the server rejects a connection with connection limit reached, the SDK rotates to the next key, recreates the session, and reconnects. With a single key, ToncenterConnectionLimitError is raised immediately.
Keys on the same plan share a combined connection limit — rotation helps when keys have independent limits (different plans).

Dynamic Subscription

WebSocket only. SSE does not support dynamic subscription changes.
After start() establishes the initial connection, ToncenterWebSocket can modify subscriptions on the fly without reconnecting. dynamic_subscribe — replace the current subscription (snapshot semantics). All previously watched addresses/traces are replaced by the new set.
dynamic_unsubscribe — remove addresses or trace hashes from the current subscription.
Both methods raise RuntimeError if called without an active WebSocket connection, and ToncenterStreamingError if the server rejects the request.

Errors

See Errors for the full exception hierarchy. Streaming-specific exceptions: All client errors except 429 are fatal — no reconnect is attempted (400, 401, 403, 404, 405, 409, 422). With a single key, catch the error manually: