Skip to main content
Use a ws:// or wss:// URL to connect over WebSocket. This gives you a persistent connection and is the only transport that supports real-time event subscriptions.

Using High-Level Clients

The transport is selected automatically based on the URL scheme.

Subscriptions

WebSocket connections can subscribe to new blocks and transactions:
Available subscription methods on CometBFT clients:

Reconnection

WebsocketClient reconnects automatically with exponential backoff (100 ms initial, doubling up to 5 seconds). Requests made while disconnected are queued and sent when the connection is restored.

Error Handler

The WebsocketClient constructor accepts an optional error handler as its second argument:
If omitted, errors are thrown by default.

Checking Subscription Support

Not all transports support event subscriptions. Only WebsocketClient implements the streaming interface. If you have a CometClient and need to check at runtime, verify the underlying RPC client is a WebsocketClient:
Calling subscribeNewBlock(), subscribeNewBlockHeader(), or subscribeTx() on a CometBFT client backed by an HTTP transport throws "This RPC client type cannot subscribe to events".

Next Steps

HTTP Transport

Use HTTP for stateless request-response queries.

Timeouts

Configure WebSocket connection and broadcast timeouts.

Connection Errors

Handle WebSocket errors and reconnection failures.