pub struct WebSocketClient { /* private fields */ }
Expand description

Tendermint RPC client that provides access to all RPC functionality (including Event subscription) over a WebSocket connection.

The WebSocketClient itself is effectively just a handle to its driver The driver is the component of the client that actually interacts with the remote RPC over the WebSocket connection. The WebSocketClient can therefore be cloned into different asynchronous contexts, effectively allowing for asynchronous access to the driver.

It is the caller’s responsibility to spawn an asynchronous task in which to execute the WebSocketClientDriver::run method. See the example below.

Dropping Subscriptions will automatically terminate them (the WebSocketClientDriver detects a disconnected channel and removes the subscription from its internal routing table). When all subscriptions to a particular query have disconnected, the driver will automatically issue an unsubscribe request to the remote RPC endpoint.

Timeouts

The WebSocket client connection times out after 30 seconds if it does not receive anything at all from the server. This will automatically return errors to all active subscriptions and terminate them.

This is not configurable at present.

Keep-Alive

The WebSocket client implements a keep-alive mechanism whereby it sends a PING message to the server every 27 seconds, matching the PING cadence of the Tendermint server (see this code for details).

This is not configurable at present.

Examples

use tendermint::abci::Transaction;
use tendermint_rpc::{WebSocketClient, SubscriptionClient, Client};
use tendermint_rpc::query::EventType;
use futures::StreamExt;

#[tokio::main]
async fn main() {
    let (client, driver) = WebSocketClient::new("ws://127.0.0.1:26657/websocket")
        .await
        .unwrap();
    let driver_handle = tokio::spawn(async move { driver.run().await });

    // Standard client functionality
    let tx = format!("some-key=some-value");
    client.broadcast_tx_async(Transaction::from(tx.into_bytes())).await.unwrap();

    // Subscription functionality
    let mut subs = client.subscribe(EventType::NewBlock.into())
        .await
        .unwrap();

    // Grab 5 NewBlock events
    let mut ev_count = 5_i32;

    while let Some(res) = subs.next().await {
        let ev = res.unwrap();
        println!("Got event: {:?}", ev);
        ev_count -= 1;
        if ev_count < 0 {
            break;
        }
    }

    // Signal to the driver to terminate.
    client.close().unwrap();
    // Await the driver's termination to ensure proper connection closure.
    let _ = driver_handle.await.unwrap();
}

Implementations

Construct a new WebSocket-based client connecting to the given Tendermint node’s RPC endpoint.

Supports both ws:// and wss:// protocols.

Construct a new WebSocket-based client connecting to the given Tendermint node’s RPC endpoint.

Supports both ws:// and wss:// protocols.

Trait Implementations

Perform a request against the RPC endpoint

/abci_info: get information about the ABCI application.

/abci_query: query the ABCI application

/block: get block at a given height.

/block: get the latest block.

/block_results: get ABCI results for a block at a particular height.

/block_results: get ABCI results for the latest block.

/block_search: search for blocks by BeginBlock and EndBlock events.

/blockchain: get block headers for min <= height <= max. Read more

/broadcast_tx_async: broadcast a transaction, returning immediately.

/broadcast_tx_sync: broadcast a transaction, returning the response from CheckTx. Read more

/broadcast_tx_commit: broadcast a transaction, returning the response from DeliverTx. Read more

/commit: get block commit at a given height.

/consensus_params: get current consensus parameters at the specified height. Read more

/consensus_state: get current consensus state

/validators: get validators a given height.

/consensus_params: get the latest consensus parameters.

/commit: get the latest block commit

/health: get node health. Read more

/genesis: get genesis file.

/net_info: obtain information about P2P and other network connections.

/status: get Tendermint status including node info, pubkey, latest block hash, app hash, block height and time. Read more

/broadcast_evidence: broadcast an evidence.

/tx: find transaction by hash.

/tx_search: search for transactions with their results.

Poll the /health endpoint until it returns a successful result or the given timeout has elapsed. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

/subscribe: subscribe to receive events produced by the given query.

/unsubscribe: unsubscribe from events relating to the given query. Read more

Subscription clients will usually have long-running underlying transports that will need to be closed at some point. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more