WebsocketClient uses a persistent WebSocket connection for both request/response
queries and event subscriptions. It is the only transport that supports
listen().
Basic Usage
/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 thesubscribe
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:Subscription Lifecycle
listen()creates a producer that sends asubscribeJSON-RPC request.- CometBFT responds with a confirmation, then pushes events with the same request ID.
- Events are filtered and emitted on the returned stream.
- When the stream has no more listeners (all
unsubscribe()), a correspondingunsubscriberequest is sent to the server to free resources. - 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:
Connection Status
You can observe connection state changes throughQueueingStreamingSocket: