Skip to main content
WebsocketClient uses a persistent WebSocket connection for both request/response queries and event subscriptions. It is the only transport that supports listen().

Basic Usage

The client automatically appends /websocket to the URL (the standard CometBFT WebSocket endpoint path).

Request/Response

execute() sends a JSON-RPC request over the WebSocket and waits for a response with a matching id. This means you can use WebsocketClient as a drop-in replacement for HttpClient when you need a persistent connection:

Subscriptions

The real power of WebSocket is event subscriptions. CometBFT supports subscribing to events like new blocks and transactions via the subscribe JSON-RPC method. WebsocketClient.listen() returns a reactive stream (xstream Stream) of SubscriptionEvent objects:

Subscribing via CometBFT Clients

The CometBFT client classes provide typed subscription helpers:
Available subscription methods:

Subscription Lifecycle

  1. listen() creates a producer that sends a subscribe JSON-RPC request.
  2. CometBFT responds with a confirmation, then pushes events with the same request ID.
  3. Events are filtered and emitted on the returned stream.
  4. When the stream has no more listeners (all unsubscribe()), a corresponding unsubscribe request is sent to the server to free resources.
  5. Streams are deduplicated by query string — subscribing to the same query twice returns the same stream.

Reconnection

WebsocketClient uses ReconnectingSocket under the hood, which provides automatic reconnection with exponential backoff. The full socket stack is:
Reconnection behavior:

Connection Status

You can observe connection state changes through QueueingStreamingSocket: