diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..2cdcf80 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,22 @@ +# Changelog + +The format is based on [Keep a Changelog]. + +[Keep a Changelog]: http://keepachangelog.com/en/1.0.0/ + +## [v0.2.0] - 2023-01-24 + +This release reworks the APIs a little bit and the major changes are: +- The subscription emits an error `DisconnectWillReconnect` once +a reconnection attempt occurs. It also contains the reason why it +was closed. +- Add API to subscribe to reconnections. +- Expose low-level APIs for subscriptions and requests. +- Upgrade jsonrpsee to v0.21. +- Rename `Client::retry_count` to `Client::reconnect_count`. +- Minor documentation tweaks. +- Modify the WebSocket ping/pong API. + +## [v0.1.0] - 2022-01-04 + +Initial release of the crate. diff --git a/README.md b/README.md index a405ef0..8a55999 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,56 @@ # reconnecting-jsonrpsee-ws-client -Wrapper crate over the jsonrpsee ws client which automatically reconnects under the hood -without that the user has to restart it manually by re-transmitting pending calls -and re-establish subscriptions that where closed when the connection was terminated. +Wrapper crate over the jsonrpsee ws client, which automatically reconnects +under the hood; without that, the user has to restart it manually by +re-transmitting pending calls and re-establish subscriptions that +were closed when the connection was terminated. -This is crate is temporary fix for subxt because it's not easy to implement -reconnect-logic on-top of it the moment. +The tricky part is subscription, which may lose a few notifications, +then re-connect where it's not possible to know which ones. -The tricky part is subscription which may loose a few notifications then re-connecting -where it's not possible to know which ones. -Lost subscription notifications may be very important to know in some scenarios then -this crate is not recommended. +Lost subscription notifications may be very important to know in some cases, +and then this library is not recommended to use. ## Example ```rust -let client = Client::builder().build(addr).await.unwrap(); -let mut sub = client.subscribe("sub".to_string(), rpc_params![], "unsub".to_string()).await.unwrap(); -let msg = sub.next().await.unwrap(); +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. + .retry_policy(ExponentialBackoff::from_millis(100)) + // 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 + .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(); +} ```