High-performance WebSocket client implementation in Rust with Python bindings. Provides both sync and async APIs with optional compatibility layer for websockets library.
For Request-Response Patterns (chat apps, API calls, gaming):
- π₯ picows: 0.034 ms RTT - Best for extreme low-latency requirements
- π₯ websocket-rs Sync: 0.128 ms RTT - Best balance of performance and simplicity
- π₯ websocket-client: 0.237 ms RTT - Good for simple sync applications
For High-Concurrency Pipelining (data streaming, batch processing):
- π₯ websocket-rs Async: 2.964 ms RTT - 12x faster than picows, 21x faster than websockets
- π₯ picows: 36.129 ms RTT - Struggles with pipelined batches
- π₯ websockets Async: 61.217 ms RTT - Pure Python limitations
WebSocket applications use two fundamentally different communication patterns:
-
Request-Response (RR): Send one message β wait for reply β send next
- Used by: Chat apps, API calls, online games, command-response systems
- Characteristics: Sequential, blocking, no concurrency
- Winner: picows (event-driven C extension)
-
Pipelined: Send multiple messages without waiting β receive all responses
- Used by: Data streaming, bulk operations, high-throughput systems
- Characteristics: Concurrent, non-blocking, batched I/O
- Winner: websocket-rs Async (Rust async with Tokio)
π View Detailed Benchmarks - Comprehensive performance comparison with test methodology
- Fixed version number sync across all package files
- Cleaned up project structure (removed obsolete python/ directory)
- Improved .gitignore rules for better precision
- Added Cargo.lock to version control
- Created separate BENCHMARKS.md for detailed performance data
Pure Sync Client - Reimplemented using tungstenite (non-async):
- Request-Response RTT: 0.128 ms (was 0.244 ms, 1.9x faster)
- 1.85x faster than websocket-client
- 6.2x faster than websockets
Architecture Design:
- Sync client: Pure blocking I/O (simple scripts, CLI tools)
- Async client: Tokio runtime (high concurrency, event-driven)
Backward Compatibility:
- 100% API compatible
- No code changes required
# From PyPI (recommended)
pip install websocket-rs
# Using uv
uv pip install websocket-rs
# From source
pip install git+https://github.com/coseto6125/websocket-rs.git# Direct usage - Sync API
from websocket_rs.sync_client import connect
with connect("ws://localhost:8765") as ws:
ws.send("Hello")
response = ws.recv()
print(response)# Direct usage - Async API
import asyncio
from websocket_rs.async_client import connect
async def main():
ws = await connect("ws://localhost:8765")
try:
await ws.send("Hello")
response = await ws.recv()
print(response)
finally:
await ws.close()
asyncio.run(main())# Monkeypatch mode (zero code changes)
import websocket_rs
websocket_rs.enable_monkeypatch()
# Existing code using websockets now uses Rust implementation
import websockets.sync.client
with websockets.sync.client.connect("ws://localhost:8765") as ws:
ws.send("Hello")
print(ws.recv())| Method | Description | Example |
|---|---|---|
connect(url) |
Create and connect WebSocket | ws = connect("ws://localhost:8765") |
send(message) |
Send message (str or bytes) | ws.send("Hello") |
recv() |
Receive message | msg = ws.recv() |
close() |
Close connection | ws.close() |
connect(
url: str, # WebSocket server URL
connect_timeout: float = 30, # Connection timeout (seconds)
receive_timeout: float = 30 # Receive timeout (seconds)
)- β Absolute lowest latency (<0.1 ms)
- β Request-response pattern (chat, API calls)
- β Team comfortable with event-driven callbacks
- β NOT for: Batch/pipelined operations
- β Simple blocking API
- β Good performance (0.1-0.13 ms RTT)
- β
Drop-in replacement for
websockets.sync - β Request-response pattern
- β 1.85x faster than websocket-client
- β NOT for: Async/await integration
- β High-concurrency pipelining
- β Batch operations (12x faster than picows)
- β Data streaming applications
- β Integration with Python asyncio
- β NOT for: Simple request-response (use Sync instead)
- β Rapid prototyping
- β Mature ecosystem
- β Comprehensive documentation
- β Low-frequency communication (<10 msg/s)
- β NOT for: High-performance requirements
# Specify version (example for Linux x86_64, Python 3.12+)
uv pip install https://github.com/coseto6125/websocket-rs/releases/download/v0.4.1/websocket_rs-0.4.1-cp312-abi3-linux_x86_64.whlRequirements:
- Python 3.12+
- Rust 1.70+ (rustup.rs)
git clone https://github.com/coseto6125/websocket-rs.git
cd websocket-rs
pip install maturin
maturin develop --release[project]
dependencies = [
"websocket-rs @ git+https://github.com/coseto6125/websocket-rs.git@main",
]# Run API compatibility tests
python tests/test_compatibility.py
# Run comprehensive benchmarks (RR + Pipelined)
python tests/benchmark_server_timestamp.py
# Run optimized benchmarks
python tests/benchmark_optimized.py# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Setup development environment
make install # Creates venv and installs dependencies
# Build and test
make dev # Development build
make test # Run tests
make bench # Run benchmarks
# Or manually with uv
uv venv
source .venv/bin/activate
uv pip install -e ".[dev]"
maturin develop --release# Install development dependencies
pip install maturin pytest websockets
# Development mode (fast iteration)
maturin develop
# Release mode (best performance)
maturin develop --release
# Watch mode (auto-recompile)
maturin develop --release --watch- Zero-cost abstractions: Rust's async/await compiles to efficient state machines
- Tokio runtime: Work-stealing scheduler optimized for I/O-bound tasks
- No GIL: True parallelism for concurrent operations
- Memory safety: No segfaults, data races, or memory leaks
Request-Response Mode:
- β PyO3 FFI overhead on every call
- β Dual runtime coordination (asyncio + Tokio)
- β Still competitive with pure Python sync
- β Better than Python async for large messages
Pipelined Mode:
- β FFI overhead amortized across batch
- β Tokio's concurrency advantages shine
- β No GIL blocking
- β Significant speedup over all Python alternatives
# Check Rust version
rustc --version # Requires >= 1.70
# Clean and rebuild
cargo clean
maturin develop --release
# Verbose mode
maturin develop --release -v- TimeoutError: Increase
connect_timeoutparameter - Module not found: Run
maturin developfirst - Connection refused: Check if server is running
- Performance not as expected: Ensure using
--releasebuild
Contributions welcome! Please ensure:
- All tests pass
- API compatibility is maintained
- Performance benchmarks included
- Documentation updated
MIT License - See LICENSE
- PyO3 - Rust Python bindings
- Tokio - Async runtime
- tokio-tungstenite - WebSocket implementation
- websockets - Python WebSocket library
- picows - High-performance Python WebSocket client