sockudo-ws
Ultra-low latency WebSocket library for Rust, designed for high-frequency trading (HFT) applications and real-time systems. Fully compatible with Tokio and Axum.
Used in Sockudo, a high-performance Pusher-compatible WebSocket server.
Coming soon: N-API bindings for Node.js
Performance
Benchmarked against uWebSockets, the industry standard for high-performance WebSockets:
| Test Case | sockudo-ws | uWebSockets | Ratio |
|---|---|---|---|
| 512 bytes, 100 connections | 232,712 msg/s | 227,973 msg/s | 1.02x |
| 1024 bytes, 100 connections | 232,072 msg/s | 224,498 msg/s | 1.03x |
| 512 bytes, 500 connections | 231,135 msg/s | 222,493 msg/s | 1.03x |
| 1024 bytes, 500 connections | 222,578 msg/s | 216,833 msg/s | 1.02x |
Benchmarked on AMD Ryzen 9 7950X, 32GB RAM, Linux 6.18
sockudo-ws matches or exceeds uWebSockets performance while providing a safe, ergonomic Rust API.
Features
- SIMD Acceleration: AVX2/AVX-512/NEON for frame masking and UTF-8 validation
- Zero-Copy Parsing: Direct buffer access without intermediate allocations
- Write Batching (Corking): Minimizes syscalls via vectored I/O
- permessage-deflate: Full compression support with shared/dedicated compressors
- Split Streams: Concurrent read/write from separate tasks
- Autobahn Compliant: Passes all 517 Autobahn test suite cases
Installation
Add to your Cargo.toml:
[]
= { = "https://github.com/RustNSparks/sockudo-ws" }
# Optional: Enable compression
= { = "https://github.com/RustNSparks/sockudo-ws", = ["permessage-deflate"] }
Quick Start
Simple Echo Server
use ;
use ;
use TcpStream;
async
Split Streams (Concurrent Read/Write)
use ;
use mpsc;
async
Axum Integration
use ;
use ;
use TokioIo;
use ;
async
Configuration (uWebSockets-style)
use ;
let config = builder
.compression // SHARED_COMPRESSOR
.max_payload_length // 16KB max message
.idle_timeout // 10 second timeout
.max_backpressure // 1MB backpressure limit
.build;
// Or use uWebSockets-style defaults
let config = uws_defaults;
Configuration Options
| Option | Default | Description |
|---|---|---|
compression |
Disabled |
Compression mode |
max_message_size |
64MB | Maximum message size |
max_frame_size |
16MB | Maximum single frame size |
idle_timeout |
120s | Close connection after inactivity (0 = disabled) |
max_backpressure |
1MB | Max write buffer before dropping connection |
auto_ping |
true | Automatic ping/pong keepalive |
ping_interval |
30s | Seconds between pings |
write_buffer_size |
16KB | Cork buffer size |
Compression Modes
| Mode | Description |
|---|---|
Compression::Disabled |
No compression |
Compression::Dedicated |
Per-connection compressor (best ratio, more memory) |
Compression::Shared |
Shared compressor (good for many connections) |
Compression::Shared4KB |
Shared with 4KB sliding window |
Compression::Shared8KB |
Shared with 8KB sliding window |
Compression::Shared16KB |
Shared with 16KB sliding window |
API Reference
WebSocketStream
The main WebSocket type implementing Stream + Sink:
// Create server-side stream
let ws = server;
// Create client-side stream
let ws = client;
// Send messages
ws.send.await?;
ws.send.await?;
// Receive messages
while let Some = ws.next.await
// Close connection
ws.close.await?;
Split Streams
For concurrent read/write operations:
let = ws.split;
// SplitReader
reader.next.await // Receive message
// SplitWriter
writer.send.await?;
writer.send_text.await?;
writer.send_binary.await?;
writer.close.await?;
writer.is_closed.await;
// Reunite
let ws = reunite?;
Message Types
Running Tests
Autobahn Test Suite
# Build and run server + tests
# Or manually:
# Then run Autobahn client in another terminal
Architecture
sockudo-ws/
├── src/
│ ├── lib.rs # Public API, Config
│ ├── stream.rs # WebSocketStream, Split types
│ ├── protocol.rs # WebSocket protocol state machine
│ ├── frame.rs # Frame encoding/decoding
│ ├── handshake.rs # HTTP upgrade handshake
│ ├── mask.rs # SIMD frame masking
│ ├── utf8.rs # SIMD UTF-8 validation
│ ├── cork.rs # Write batching buffer
│ ├── deflate.rs # permessage-deflate compression
│ └── simd.rs # SIMD feature detection
├── examples/
│ ├── simple_echo.rs # Basic echo server
│ ├── split_echo.rs # Concurrent read/write example
│ └── axum_echo.rs # Axum integration example
├── autobahn/
│ ├── server.rs # Autobahn test server
│ └── Makefile # Build and test automation
└── benches/
└── throughput.rs # Criterion benchmarks
Performance Optimizations
- SIMD Masking: Uses AVX2/AVX-512/NEON to XOR mask frames at 32-64 bytes per cycle
- SIMD UTF-8: Validates UTF-8 text at memory bandwidth speeds
- Zero-Copy: Parses frames directly from receive buffer without copying
- Cork Buffer: Batches small writes into 16KB chunks for fewer syscalls
- Vectored I/O: Uses
writev()to send multiple buffers in single syscall
License
MIT
Credits
- Inspired by uWebSockets