#websocket-client #wss #client

fast_websocket_client

Tokio-native WebSocket client for Rust. High-throughput, low-latency, callback-driven, proxy-ready.

9 unstable releases (3 breaking)

Uses new Rust 2024

0.4.1 Oct 19, 2025
0.4.0 Jul 20, 2025
0.3.1 May 23, 2025
0.3.0 Apr 6, 2025
0.1.2 Aug 22, 2023

#28 in WebSocket

Download history 1453/week @ 2025-07-15 1031/week @ 2025-07-22 647/week @ 2025-07-29 886/week @ 2025-08-05 1546/week @ 2025-08-12 830/week @ 2025-08-19 1756/week @ 2025-08-26 1348/week @ 2025-09-02 1273/week @ 2025-09-09 1001/week @ 2025-09-16 1376/week @ 2025-09-23 1240/week @ 2025-09-30 1220/week @ 2025-10-07 1357/week @ 2025-10-14 886/week @ 2025-10-21 1047/week @ 2025-10-28

4,721 downloads per month

Apache-2.0

53KB
807 lines

fast_websocket_client

Crates.io docs.rs

Tokio-native WebSocket client for Rust – high-throughput, low-latency, callback-driven, proxy-ready.

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

  • JavaScript-like callback API for event handling
  • Async/await support via tokio
  • Powered by fastwebsockets
  • Built-in reconnection and ping loop
  • TLS support
  • Custom HTTP headers for handshake (e.g., Authorization)
  • Proxy support (HTTP CONNECT & SOCKS5)

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_open(|_| async move {
        println!("[OPEN] WebSocket connection opened.");
    })
    .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(2)).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(2)).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

~17–39MB
~732K SLoC