[][src]Struct tendermint_rpc::WebSocketClient

pub struct WebSocketClient { /* fields omitted */ }

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 (see the new method). 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 driver's 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("tcp://127.0.0.1:26657".parse().unwrap())
        .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

impl WebSocketClient[src]

pub async fn new(address: Address) -> Result<(Self, WebSocketClientDriver)>[src]

Construct a WebSocket client. Immediately attempts to open a WebSocket connection to the node with the given address.

On success, this returns both a client handle (a WebSocketClient instance) as well as the WebSocket connection driver. The execution of this driver becomes the responsibility of the client owner, and must be executed in a separate asynchronous context to the client to ensure it doesn't block the client.

pub fn close(self) -> Result<()>[src]

Signals to the driver that it must terminate.

Trait Implementations

impl Client for WebSocketClient[src]

impl Clone for WebSocketClient[src]

impl Debug for WebSocketClient[src]

impl SubscriptionClient for WebSocketClient[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,