Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

WebSocket Support

Ultimo provides zero-dependency, RFC 6455 compliant WebSocket support with built-in pub/sub functionality.

Features

  • ✅ Zero Dependencies - Built directly on hyper's upgrade mechanism
  • ✅ Type-Safe - WebSocketHandler trait with typed context data
  • ✅ Built-in Pub/Sub - Topic-based messaging with ChannelManager
  • ✅ Production Ready - 93 comprehensive tests, fully compliant with RFC 6455
  • ✅ Easy Integration - Seamless router integration with app.websocket()
  • ✅ Compression - Optional per-message deflate (RFC 7692), negotiated with browsers automatically

Quick Start

Basic WebSocket Handler

use ultimo::prelude::*;
use ultimo::websocket::{WebSocketHandler, WebSocket, Message};
use async_trait::async_trait;
 
#[derive(Clone)]
struct ChatHandler;
 
#[async_trait]
impl WebSocketHandler for ChatHandler {
    type Data = ();
 
    async fn on_open(&self, ws: &WebSocket<Self::Data>) {
        println!("Client connected");
        ws.send("Welcome to the chat!").await.ok();
    }
 
    async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
        match msg {
            Message::Text(text) => {
                println!("Received: {}", text);
                ws.send(format!("Echo: {}", text)).await.ok();
            }
            Message::Close(_) => {
                println!("Client disconnected");
            }
            _ => {}
        }
    }
}
 
#[tokio::main]
async fn main() {
    let mut app = Ultimo::new();
    
    // Add WebSocket route
    app.websocket("/ws", ChatHandler);
    
    app.listen("127.0.0.1:3000").await.unwrap();
}

Client Connection

const ws = new WebSocket("ws://localhost:3000/ws");
 
ws.addEventListener("open", () => {
  console.log("Connected!");
  ws.send("Hello, server!");
});
 
ws.addEventListener("message", (event) => {
  console.log("Received:", event.data);
});

Connect directly to the backend origin like this in development, rather than through a frontend dev server's proxy (e.g. Vite's server.proxy) — those proxies are built for short-lived request/response traffic and don't handle long-lived WebSocket/SSE connections well. See SSE → Vite dev proxy buffers streaming responses for the full explanation (same underlying issue).

Typed Context Data

WebSocket connections can carry typed context data:

use serde::{Serialize, Deserialize};
 
#[derive(Clone, Serialize, Deserialize)]
struct UserContext {
    user_id: String,
    username: String,
}
 
#[derive(Clone)]
struct AuthHandler;
 
#[async_trait]
impl WebSocketHandler for AuthHandler {
    type Data = UserContext;
 
    async fn on_open(&self, ws: &WebSocket<Self::Data>) {
        let user = ws.data();
        println!("User {} connected", user.username);
        
        ws.send(format!("Welcome, {}!", user.username)).await.ok();
    }
 
    async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
        let user = ws.data();
        // Access user context in message handler
        println!("Message from {}: {:?}", user.username, msg);
    }
}

Pub/Sub System

Built-in topic-based pub/sub for broadcasting messages:

#[derive(Clone)]
struct ChatRoomHandler;
 
#[async_trait]
impl WebSocketHandler for ChatRoomHandler {
    type Data = String; // User ID
 
    async fn on_open(&self, ws: &WebSocket<Self::Data>) {
        let user_id = ws.data();
        
        // Subscribe to chat room
        ws.subscribe("chat:general").await.ok();
        
        // Announce join
        let msg = serde_json::json!({
            "type": "join",
            "user_id": user_id,
        });
        ws.publish("chat:general", &msg).await.ok();
    }
 
    async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
        if let Message::Text(text) = msg {
            let user_id = ws.data();
            
            // Broadcast to all subscribers
            let msg = serde_json::json!({
                "type": "message",
                "user_id": user_id,
                "text": text,
            });
            ws.publish("chat:general", &msg).await.ok();
        }
    }
 
    async fn on_close(&self, ws: &WebSocket<Self::Data>, _code: u16, _reason: &str) {
        let user_id = ws.data();
        
        // Announce leave
        let msg = serde_json::json!({
            "type": "leave",
            "user_id": user_id,
        });
        ws.publish("chat:general", &msg).await.ok();
        
        // Cleanup
        ws.unsubscribe("chat:general").await.ok();
    }
}

Lifecycle Callbacks

WebSocketHandler provides several lifecycle hooks:

#[async_trait]
impl WebSocketHandler for MyHandler {
    type Data = ();
 
    // Called when connection is established
    async fn on_open(&self, ws: &WebSocket<Self::Data>) {
        // Initialize connection
    }
 
    // Called when message is received
    async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
        // Handle incoming messages
    }
 
    // Called when connection is closed
    async fn on_close(&self, ws: &WebSocket<Self::Data>, code: u16, reason: &str) {
        // Cleanup resources
    }
}

Message Types

WebSocket supports multiple message types:

use ultimo::websocket::Message;
 
async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
    match msg {
        Message::Text(text) => {
            println!("Text message: {}", text);
        }
        Message::Binary(data) => {
            println!("Binary message: {} bytes", data.len());
        }
        Message::Ping(data) => {
            // Pings are automatically handled
            println!("Ping received");
        }
        Message::Pong(data) => {
            println!("Pong received");
        }
        Message::Close(frame) => {
            if let Some(f) = frame {
                println!("Close: code={}, reason={}", f.code, f.reason);
            }
        }
    }
}

JSON Messages

Send and receive JSON messages easily:

use serde::{Serialize, Deserialize};
 
#[derive(Serialize, Deserialize)]
struct ChatMessage {
    user: String,
    text: String,
    timestamp: u64,
}
 
async fn on_message(&self, ws: &WebSocket<Self::Data>, msg: Message) {
    if let Message::Text(text) = msg {
        // Parse JSON
        if let Ok(chat_msg) = serde_json::from_str::<ChatMessage>(&text) {
            println!("{} says: {}", chat_msg.user, chat_msg.text);
            
            // Send JSON response
            let response = ChatMessage {
                user: "Server".to_string(),
                text: "Message received".to_string(),
                timestamp: now(),
            };
            ws.send_json(&response).await.ok();
        }
    }
}

Examples

Check out the complete examples in the repository:

Simple Chat

A basic HTML/JS chat application demonstrating real-time messaging and pub/sub.

cargo run -p websocket-chat

React Chat

Modern React + TypeScript chat with shadcn/ui components, featuring:

  • Type-safe WebSocket hook
  • Real-time message broadcasting
  • Connection status indicators
  • Message history with auto-scroll
cargo run -p websocket-chat-react

Compression

With the websocket-compression feature, Ultimo supports per-message deflate (RFC 7692), the WebSocket compression every major browser asks for. Turn it on per route:

[dependencies]
ultimo = { version = "0.10", features = ["websocket-compression"] }
use ultimo::websocket::WebSocketConfig;
 
let config = WebSocketConfig {
    compression: true,
    ..Default::default()
};
app.websocket_with_config("/ws", ChatHandler, config);

Nothing changes in your handler or in the browser: new WebSocket(url) offers compression on its own, the server accepts during the handshake, and messages are compressed and decompressed transparently. Clients that don't offer it get an uncompressed connection as before.

How it behaves
  • Text and binary messages of 32 bytes or more are compressed. Smaller ones go uncompressed, where the compression overhead would outweigh any saving.
  • Messages share a 32 KiB window, so repeated keys and values compress to almost nothing. On a stream of ~100-byte JSON messages (price ticks and chat messages) we measured 63% fewer bytes at about 3 µs per message to compress and the same to decompress. Larger, more repetitive payloads compress further. If the client asks for server_no_context_takeover, each message is compressed on its own instead, which saves far less.
  • max_message_size applies to the decompressed size. A small message that would inflate beyond it — a decompression bomb — is refused with close code 1009, without inflating it in full. Corrupt compressed data closes the connection with 1007.

The trade-off is memory. A connection that has sent and received compressed messages holds about 350 KiB for the compressor and decompressor (each is created the first time it's needed). That's negligible for hundreds of connections, but at 10,000 connections it's about 3.5 GB. For very large connection counts with small messages, measure before turning it on.

Building with compression: true but without the websocket-compression feature logs a warning once and serves uncompressed connections.

Architecture

Ultimo's WebSocket implementation:

  • Frame Codec - RFC 6455 compliant frame parsing and encoding
  • Connection - WebSocket connection state management
  • Upgrade - HTTP to WebSocket upgrade mechanism
  • Pub/Sub - ChannelManager for topic-based messaging
  • Router - Optimized Radix Tree for efficient route matching

Security

Cross-Site WebSocket Hijacking (CSWSH)

Browsers don't enforce same-origin for WebSocket connections the way they do for fetch/XHR — a page on any origin can open a WebSocket connection to your server. If the connection relies on ambient cookie authentication, restrict the handshake's Origin header with an allow-list:

app.websocket_with_config_and_origins(
    "/ws",
    ChatHandler,
    WebSocketConfig::default(),
    vec!["https://example.com".to_string()],
);

The allow-list is empty by default (no restriction — matches websocket_with_config's prior behavior). Set it whenever the connection carries ambient authority. "*" matches any origin; otherwise the match is exact against the full Origin value. Once set, a handshake missing the Origin header is rejected with 403 Forbidden.

Testing

The WebSocket implementation includes 93 comprehensive tests:

  • 21 unit tests (frame codec, pub/sub, upgrade)
  • 9 integration tests (real connections, JSON, pub/sub)
  • 12 property-based tests (thousands of random test cases)
  • 5 router integration tests
  • 14 error handling tests
  • 11 concurrency tests
  • 18 edge case tests

Performance

  • Zero Dependencies - No tokio-tungstenite or similar libraries (compression adds the pure-Rust flate2, only with websocket-compression)
  • Efficient - O(L) router lookups with Radix Tree
  • Memory - Minimal per-connection overhead
  • Tested - 210 tests passing, production-ready

Learn More