Giter Club home page Giter Club logo

durable-ws-client's Introduction

durable-ws-client

A reconnecting websocket client written in Rust, using tokio-tungstenite.

The client (located in src/client.rs) will attempt to reconnect on any error, while informing the application of the error so it can permanently terminate the connection if desired.

Example usage

Requires an entry along the lines of tokio = { version = "1.21.2", features = ["rt-multi-thread", "macros", "sync"] } in your Cargo.toml to work, along with some kind of TLS feature enabled on this library, for example "native-tls".

use durable_ws_client::{
    backoff::FixedBackoff,
    client::{NewConnection, ReconnectingClient},
    config::Config,
    connection::WebSocketBuilder,
};
use std::time::Duration;
use tokio::{join, sync::mpsc};

#[tokio::main]
async fn main() {
    let websocket = WebSocketBuilder;
    let backoff = FixedBackoff::new(Duration::from_secs(1));

    let (new_connection_tx, mut new_connection_rx) = mpsc::channel(1);

    let client_task = async move {
        ReconnectingClient::connect(
            Config::default(),
            "wss://www.bitmex.com/realtime".parse().unwrap(),
            websocket,
            backoff,
            new_connection_tx,
        )
        .await;
    };

    let reader_task = async move {
        let NewConnection {
            channels: (tx, mut rx),
            connection_closed,
        } = new_connection_rx.recv().await.unwrap().unwrap();

        println!("message received: {:?}", rx.recv().await.unwrap());

        drop(tx);
        drop(rx);

        if let Ok(on_close) = connection_closed.await {
            println!("close data received: {:?}", on_close);
        }
    };

    join!(client_task, reader_task);
}

durable-ws-client's People

Contributors

douglasgray avatar

Stargazers

Shabbir Hasan avatar Nicolas Marshall avatar Sammy Shi avatar qdrs avatar

Watchers

James Cloos avatar  avatar

Forkers

shabbirhasan1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.