Client
- SSE
- WebSocket
Session
start — builds the subscription from registered handlers and dispatches notifications. Blocks untilstop() 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.
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 afinality field. The lifecycle is monotonic per trace: pending → confirmed → finalized. 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.
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.Dynamic Subscription
WebSocket only. SSE does not support dynamic subscription changes.
start() establishes the initial connection, TonapiWebSocket 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 TONAPIStreamingError 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).