#fast-websocket #axum #websocket-server #hpx

hpx-fastwebsockets

A fast RFC6455 WebSocket server implementation

10 releases (5 stable)

Uses new Rust 2024

new 1.4.0 Feb 13, 2026
1.3.0 Feb 8, 2026
0.1.5 Feb 3, 2026
0.1.4 Jan 13, 2026

#163 in WebSocket


Used in 2 crates

Apache-2.0

75KB
1.5K SLoC

hpx-fastwebsockets

crates.io docs.rs License

A fast, minimal RFC 6455 WebSocket implementation.

This crate is part of the hpx project. It is a fork of fastwebsockets by Divy Srivastava.

Passes the Autobahn test suite and fuzzed with LLVM's libfuzzer. Can be used as a raw WebSocket frame parser or as a full-fledged WebSocket server/client.

Quick Start

use tokio::net::TcpStream;
use hpx_fastwebsockets::{WebSocket, OpCode, Role};
use eyre::Result;

async fn handle(mut ws: WebSocket<TcpStream>) -> Result<()> {
    loop {
        let frame = ws.read_frame().await?;
        match frame.opcode {
            OpCode::Close => break,
            OpCode::Text | OpCode::Binary => {
                ws.write_frame(frame).await?;
            }
            _ => {}
        }
    }
    Ok(())
}

HTTP Upgrade (with hyper)

use hpx_fastwebsockets::upgrade;
use hyper::{Request, Response, body::Incoming};
use http_body_util::Empty;
use hyper::body::Bytes;

async fn server_upgrade(
    mut req: Request<Incoming>,
) -> Result<Response<Empty<Bytes>>, hpx_fastwebsockets::WebSocketError> {
    let (response, fut) = upgrade::upgrade(&mut req)?;

    tokio::spawn(async move {
        let mut ws = fut
            .await
            .expect("Failed to complete WebSocket upgrade");
        // Use ws.read_frame() / ws.write_frame() ...
    });

    Ok(response)
}

Feature Flags

Feature Default Description
simd Yes SIMD-accelerated UTF-8 validation
upgrade Yes HTTP upgrade support (requires hyper)
unstable-split No Split WebSocket into read/write halves
with_axum No Axum WebSocket extractor integration

Key Types

  • WebSocket<S> — Main WebSocket struct for reading/writing frames
  • Frame — A WebSocket frame with opcode and payload
  • OpCode — Frame opcodes (Text, Binary, Close, Ping, Pong, Continuation)
  • FragmentCollector<S> — Aggregates fragmented messages into complete frames
  • Role — Server or Client role (controls masking behavior)

License

Apache-2.0

Dependencies

~2.5–4.5MB
~71K SLoC