#websocket-client #client #wss #async #tokio

fast_websocket_client

A fast asynchronous websocket client built on top of fastwebsockets library

6 releases

Uses new Rust 2024

0.3.0 Apr 6, 2025
0.2.0 Feb 8, 2024
0.1.3 Dec 22, 2023
0.1.2 Aug 22, 2023

#73 in WebSocket

Download history 3/week @ 2024-12-25 27/week @ 2025-01-01 49/week @ 2025-01-08 23/week @ 2025-01-15 78/week @ 2025-01-22 24/week @ 2025-01-29 4/week @ 2025-02-05 4/week @ 2025-02-12 15/week @ 2025-02-19 33/week @ 2025-02-26 32/week @ 2025-03-05 18/week @ 2025-03-12 106/week @ 2025-04-02 26/week @ 2025-04-09

135 downloads per month

Apache-2.0

40KB
614 lines

fast_websocket_client

Crates.io docs.rs

A blazing-fast, async-native WebSocket client for Rust, built on top of fastwebsockets and tokio.

Supports two modes of operation:

  • ๐Ÿ” High-level callback-based client for ergonomic event-driven use.
  • โš™๏ธ Low-level direct API for fine-tuned control with minimal dependencies.

Quick Example: examples/async_callback_client.rs

๐Ÿ“ฆ Features

  • Async/await support via tokio
  • Built-in reconnection and ping loop
  • Optional callback-driven lifecycle management
  • Custom HTTP headers for handshake (e.g., Authorization)

๐Ÿ›  Installation

cargo add fast_websocket_client

๐Ÿ” High-Level Callback API

An ergonomic, JavaScript-like API with built-in reconnect, ping, and lifecycle hooks.

// try this example with
// `cargo run --example wss_client`

use tokio::time::{Duration, sleep};
use fast_websocket_client::WebSocket;

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), fast_websocket_client::WebSocketClientError> {
    let ws = WebSocket::new("wss://echo.websocket.org").await?;

    ws.on_close(|_| async move {
        println!("[CLOSE] WebSocket connection closed.");
    })
    .await;
    ws.on_message(|message| async move {
        println!("[MESSAGE] {}", message);
    })
    .await;

    sleep(Duration::from_secs(1)).await;
    for i in 1..5 {
        let message = format!("#{}", i);
        if let Err(e) = ws.send(&message).await {
            eprintln!("[ERROR] Send error: {:?}", e);
            break;
        }
        println!("[SEND] {}", message);
        sleep(Duration::from_secs(5)).await;
    }

    ws.close().await;
    ws.await_shutdown().await;
    Ok(())
}

๐Ÿงต Low-Level API

use fast_websocket_client::{connect, OpCode};

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
    let mut client = connect("wss://echo.websocket.org").await?;

    client.send_string("Hello, WebSocket!").await?;

    let frame = client.receive_frame().await?;
    if frame.opcode == OpCode::Text {
        println!("Received: {}", String::from_utf8_lossy(&frame.payload));
    }

    client.send_close("bye").await?;
    Ok(())
}

๐Ÿงช Running the Example

Clone the repo and run:

cargo run --example wss_client

๐Ÿ”„ Migration Guide (from 0.2.0)

Old New
client::Offline base_client::Offline
client::Online base_client::Online
Runtime settings via Online's methods Must now be set before connect via ConnectionInitOptions.
Changes to the running WebSocket take effect on the next (re)connection.

New users: We recommend starting with the WebSocket API for best experience.

๐Ÿ“š Documentation


๐Ÿ’ก Actively maintained โ€“ contributions are welcome!

Dependencies

~18โ€“28MB
~492K SLoC