Crate sockudo_ws

Crate sockudo_ws 

Source
Expand description

§Sockudo-WS: Ultra-low latency WebSocket library

A high-performance WebSocket library designed for HFT applications, fully compatible with Tokio and Axum.

§Performance Features

  • SIMD Acceleration: AVX2/AVX-512/NEON for frame masking and UTF-8 validation
  • Zero-Copy Parsing: Direct buffer access without intermediate copies
  • Write Batching (Corking): Minimizes syscalls via vectored I/O
  • Cache-Line Alignment: Prevents false sharing in concurrent scenarios
  • Lock-Free Queues: SPSC/MPMC for cross-task communication
  • Optional mimalloc: High-performance allocator for reduced latency

§Example with Axum

use axum::{Router, routing::get};
use sockudo_ws::axum::WebSocketUpgrade;

async fn ws_handler(ws: WebSocketUpgrade) -> impl IntoResponse {
    ws.on_upgrade(|socket| async move {
        // Handle WebSocket connection
    })
}

let app = Router::new().route("/ws", get(ws_handler));

§HTTP/2 and HTTP/3 WebSocket Support

use sockudo_ws::{WebSocketServer, WebSocketClient, Http2, Http3, Config};

// HTTP/2 server
let server = WebSocketServer::<Http2>::new(Config::default());
server.serve(tls_stream, |ws, req| async move {
    // handle connection
}).await?;

// HTTP/3 server
let server = WebSocketServer::<Http3>::bind(addr, tls_config, Config::default()).await?;
server.serve(|ws, req| async move {
    // handle connection
}).await?;

Re-exports§

pub use error::Error;
pub use error::Result;
pub use frame::Frame;
pub use frame::OpCode;
pub use handshake::HandshakeResult;
pub use protocol::Message;
pub use protocol::Role;
pub use pubsub::PubSub;
pub use pubsub::PubSubState;
pub use pubsub::PublishResult;
pub use pubsub::SubscriberId;
pub use stream::SplitReader;
pub use stream::SplitWriter;
pub use stream::Stream;
pub use stream::WebSocketStream;
pub use stream::CompressedWebSocketStream;
pub use transport::Http1;
pub use transport::Http2;
pub use transport::Http3;
pub use transport::Transport;
pub use compression::CompressionContext;
pub use compression::SharedCompressorPool;
pub use compression::global_shared_pool;
pub use deflate::DeflateConfig;
pub use deflate::DeflateContext;
pub use protocol::CompressedProtocol;

Modules§

client
Unified WebSocket client implementation
compression
Compression management for WebSocket connections
cork
Write batching (corking) mechanism
deflate
Per-Message Deflate Extension (RFC 7692)
error
Error types for the WebSocket library
frame
WebSocket frame parsing and serialization
handshake
WebSocket handshake implementation
mask
WebSocket frame masking utilities
prelude
Prelude module for convenient imports
protocol
WebSocket protocol implementation
pubsub
High-performance pub/sub system for WebSocket connections
queue
Lock-free SPSC and MPMC queues for the event loop
server
Unified WebSocket server implementation
simd
SIMD-accelerated operations for WebSocket processing
stream
Async WebSocket stream implementation
transport
Transport markers for HTTP/1.1, HTTP/2, and HTTP/3 WebSocket connections
utf8
SIMD-accelerated UTF-8 validation

Structs§

Config
Configuration for WebSocket connections
ConfigBuilder
Builder for WebSocket configuration

Enums§

Compression
Compression mode for WebSocket connections (RFC 7692 permessage-deflate)

Constants§

CACHE_LINE_SIZE
Cache line size for modern CPUs (64 bytes for x86_64, ARM64)
CORK_BUFFER_SIZE
Default cork buffer size (16KB like uWebSockets)
MAX_FRAME_HEADER_SIZE
Maximum WebSocket frame header size (2 + 8 + 4 = 14 bytes)
MEDIUM_MESSAGE_THRESHOLD
Medium message threshold (< 64KB uses 4-byte header)
RECV_BUFFER_SIZE
Default receive buffer size (64KB for high throughput)
SMALL_MESSAGE_THRESHOLD
Small message threshold for fast-path optimization (< 126 bytes uses 2-byte header)
WS_GUID
WebSocket GUID for handshake