Skip to content

Websocket wrapper library based on Tokio-Tungstenite to provide an ergonomic interface for working with streaming services

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

zheylmun/socketeer

Repository files navigation

socketeer

Crates.io Version docs.rs GitHub branch status Codecov

socketeer is a small wrapper which creates a tokio-tungstenite websocket connection and exposes a simplified async API to send and receive application messages. It automatically handles the underlying websocket and allows for reconnection, as well as immediate handling of errors in client code. Currently supports only json encoded binary and text messages.

Usage

use socketeer::Socketeer;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct SocketMessage {
    message: String,
}

#[tokio::main]
async fn main() {
    // Create a Socketeer instance that connects to a fictional local server.
    let mut socketeer: Socketeer<SocketMessage, SocketMessage> =
        Socketeer::connect("ws://127.0.0.1:80")
            .await
            .unwrap();
    // Send a message to the server
    socketeer
        .send(SocketMessage {
            message: "Hello, world!".to_string(),
        })
        .await
        .unwrap();
    // Wait for the server to respond
    let response = socketeer.next_message().await.unwrap();
    println!("{response:#?}");
    // Shut everything down and close the connection
    socketeer.close_connection().await.unwrap();
}

About

Websocket wrapper library based on Tokio-Tungstenite to provide an ergonomic interface for working with streaming services

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages