📖 WS-RS Guide

CA: BHUxWbRJbSFsRW2JSMzm9zCJQGXkmeKhVxUWDbrHpump

Getting Started with WS-RS

WS-RS is a lightweight, fast, and reliable WebSocket server library for Rust.

Installation

Add WS-RS to your project:

[dependencies]
ws-rs = "0.9"
tokio = { version = "1", features = ["full"] }

Quick Start

Create a basic WebSocket server:

use ws_rs::WebSocketServer;

#[tokio::main]
async fn main() -> Result<(), Box> {
    let server = WebSocketServer::new("127.0.0.1:8080");
    
    server.on_connect(|client_id| {
        println!("Client {} connected", client_id);
    });
    
    server.on_message(|client_id, message| {
        println!("Received from {}: {}", client_id, message);
    });
    
    server.on_disconnect(|client_id| {
        println!("Client {} disconnected", client_id);
    });
    
    println!("WebSocket server running on ws://127.0.0.1:8080");
    server.start().await?;
    Ok(())
}

Client Connection

Connect to your WebSocket server using JavaScript:

const ws = new WebSocket('ws://localhost:8080');

ws.onopen = () => {
    console.log('Connected to server');
    ws.send('Hello Server!');
};

ws.onmessage = (event) => {
    console.log('Received:', event.data);
};

ws.onclose = () => {
    console.log('Disconnected from server');
};

Features

  • High Performance - Built with Tokio for async I/O
  • RFC 6455 Compliant - Full WebSocket protocol support
  • Secure WebSockets (WSS) - TLS/SSL support
  • Message Compression - Per-message deflate extension
  • Broadcasting - Send messages to multiple clients

Advanced Topics