#json-rpc #json-rpc-client #websocket #websocket-client #rpc-client #json

reconnecting-jsonrpsee-ws-client

jsonrpc-ws-client that reconnects automatically. Warning: It may lose subscription messages when reconnecting.

7 unstable releases (3 breaking)

0.4.3 Jun 25, 2024
0.4.2 Apr 24, 2024
0.3.0 Feb 8, 2024
0.2.0 Jan 24, 2024
0.1.0 Jan 4, 2024

#286 in Network programming

Download history 1657/week @ 2024-07-10 1261/week @ 2024-07-17 1192/week @ 2024-07-24 1267/week @ 2024-07-31 2246/week @ 2024-08-07 2331/week @ 2024-08-14 2783/week @ 2024-08-21 2155/week @ 2024-08-28 2223/week @ 2024-09-04 6949/week @ 2024-09-11 28890/week @ 2024-09-18 25628/week @ 2024-09-25 33764/week @ 2024-10-02 31376/week @ 2024-10-09 35745/week @ 2024-10-16 33780/week @ 2024-10-23

138,916 downloads per month

MIT license

58KB
1K SLoC

reconnecting-jsonrpsee-ws-client

Wrapper crate over the jsonrpsee ws client, which automatically reconnects under the hood; without that, one has to restart it. It supports a few retry strategies, such as exponential backoff, but it's also possible to use custom strategies as long as it implements Iterator<Item = Duration>.

By default, the library is re-transmitting pending calls and re-establishing subscriptions that were closed when the connection was terminated, but it's also possible to disable that and manage it yourself.

For instance, you may not want to re-subscribe to a subscription that has side effects or retries at all. Then the library exposes request_with_policy and subscribe_with_policy to support that

    let mut sub = client
        .subscribe_with_policy(
            "subscribe_lo".to_string(),
            rpc_params![],
            "unsubscribe_lo".to_string(),
            // Do not re-subscribe if the connection is closed.
            CallRetryPolicy::Retry,
        )
        .await
        .unwrap();

The tricky part is subscriptions, which may lose a few notifications when it's re-connecting, it's not possible to know which ones.

Lost subscription notifications may be very important to know in some cases, and then this library is not recommended to use.

There is one way to determine how long a reconnection takes:

    // Print when the RPC client starts to reconnect.
    loop {
        rpc.reconnect_started().await;
        let now = std::time::Instant::now();
        rpc.reconnected().await;
        println!(
            "RPC client reconnection took `{} seconds`",
            now.elapsed().as_secs()
        );
    }

Example

use reconnecting_jsonrpsee_ws_client::{rpc_params, Client, ExponentialBackoff, PingConfig};
use std::time::Duration;

async fn run() {
    // Create a new client with with a reconnecting RPC client.
    let client = Client::builder()
        // Reconnect with exponential backoff and if fails more then 
        // 10 retries we give up and terminate.
        .retry_policy(ExponentialBackoff::from_millis(100).take(10))
        // Send period WebSocket pings/pongs every 6th second and
        // if ACK:ed in 30 seconds then disconnect.
        //
        // This is just a way to ensure that the connection isn't
        // idle if no message is sent that often
        //
        // This only works for native.
        .enable_ws_ping(
            PingConfig::new()
                .ping_interval(Duration::from_secs(6))
                .inactive_limit(Duration::from_secs(30)),
        )
        .build("ws://localhost:9944".to_string())
        .await
        .unwrap();

    // make a JSON-RPC call
    let json = client
        .request("say_hello".to_string(), rpc_params![])
        .await
        .unwrap();

    // make JSON-RPC subscription.
    let mut sub = client
        .subscribe(
            "subscribe_lo".to_string(),
            rpc_params![],
            "unsubscribe_lo".to_string(),
        )
        .await
        .unwrap();
    let notif = sub.next().await.unwrap();
}

Dependencies

~4–15MB
~179K SLoC