4 releases

new 0.1.0 Jan 6, 2025
0.0.3 Dec 31, 2024
0.0.2 Dec 28, 2024
0.0.1 Dec 28, 2024

#96 in WebSocket

Download history 298/week @ 2024-12-25 156/week @ 2025-01-01

454 downloads per month

MIT/Apache

27KB
482 lines

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();
}

Dependencies

~6–18MB
~249K SLoC