[][src]Struct tendermint_rpc::WebSocketClient

pub struct WebSocketClient { /* fields omitted */ }

Tendermint RPC client that provides Event subscription capabilities over JSON-RPC over a WebSocket connection.

In order to not block the calling task, this client spawns an asynchronous driver that continuously interacts with the actual WebSocket connection. The WebSocketClient itself is effectively just a handle to this driver. This driver is spawned as the client is created.

To terminate the client and the driver, simply use its close method.

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

This example is not tested
use tendermint_rpc::{WebSocketClient, SubscriptionClient};
use tendermint_rpc::query::EventType;
use futures::StreamExt;

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

    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
        }
    }

    // Sends an unsubscribe request via the WebSocket connection, but keeps
    // the connection open.
    subs.terminate().await.unwrap();

    // Signal to the driver to terminate.
    client.close().await.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 async fn close(__arg0: Self) -> Result<()>[src]

Signals to the driver that it must terminate.

Trait Implementations

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>,