Skip to main content

Rust-powered WebSocket engine for Python. Up to 14M msg/s JSON, 30M binary (MsgPack/zlib). Zero-GIL JWT. E2E encrypted.

Project description

WSE -- WebSocket Engine

A complete, out-of-the-box solution for real-time communication between React, Python, and backend services.

Three packages. Four lines of code. Your frontend and backend talk in real time.

CI PyPI - Server PyPI - Client npm License: MIT


Why WSE?

Building real-time features between React and Python is painful. You need WebSocket handling, reconnection logic, message ordering, authentication, encryption, offline support, health monitoring. That's weeks of work before you ship a single feature.

WSE gives you all of this out of the box.

Install wse-server on your backend, wse-client on your frontend (React or Python). Everything works immediately: auto-reconnection, message encryption, sequence ordering, offline queues, health monitoring. No configuration required for the defaults. Override what you need.

The engine is Rust-accelerated via PyO3. Up to 14M msg/s JSON, 30M msg/s compressed — benchmarked on AMD EPYC 7502P (32 cores) with the Rust stress-test client (results). Sub-millisecond connection latency (0.38ms median) with Rust JWT authentication.


Quick Start

Server (Python) -- Router Mode

Embed WSE into your existing FastAPI app on the same port:

from fastapi import FastAPI
from wse_server import create_wse_router, WSEConfig

app = FastAPI()

wse = create_wse_router(WSEConfig(
    redis_url="redis://localhost:6379",
))

app.include_router(wse, prefix="/wse")

# Publish from anywhere in your app
await wse.publish("notifications", {"text": "Order shipped!", "order_id": 42})

Server (Python) -- Standalone Mode

Run the Rust WebSocket server on a dedicated port for maximum throughput:

from wse_server._wse_accel import RustWSEServer

server = RustWSEServer(
    "0.0.0.0", 5006,
    max_connections=10000,
    jwt_secret=b"your-secret-key",     # Rust JWT validation in handshake
    jwt_issuer="your-app",
    jwt_audience="your-api",
)
server.start()

# Drain inbound events in a background thread
while True:
    events = server.drain_inbound(256, 50)  # batch size, timeout ms
    for event in events:
        handle(event)

Standalone mode gives you a dedicated Rust tokio runtime on its own port -- no FastAPI overhead, no GIL on the hot path. This is how WSE achieves 14M msg/s on JSON.

Client (React)

import { useWSE } from 'wse-client';

function Dashboard() {
  const { isConnected, connectionHealth } = useWSE({
    topics: ['notifications', 'live_data'],
    endpoints: ['ws://localhost:8000/wse'],
  });

  useEffect(() => {
    const handler = (e: CustomEvent) => {
      console.log('New notification:', e.detail);
    };
    window.addEventListener('notifications', handler);
    return () => window.removeEventListener('notifications', handler);
  }, []);

  return <div>Status: {connectionHealth}</div>;
}

That's it. Your React app receives real-time updates from your Python backend.

Client (Python)

from wse_client import connect

async with connect("ws://localhost:5006/wse", token="your-jwt") as client:
    await client.subscribe(["notifications", "live_data"])
    async for event in client:
        print(event.type, event.payload)

Same wire protocol, same features. Use the Python client for backend-to-backend communication, microservices, CLI tools, integration tests, or any non-browser use case.


What You Get Out of the Box

Everything listed below works the moment you install. No extra setup.

Reactive Interface

Real-time data flow from Python to React. One hook (useWSE) on the client, one publish() call on the server. Events appear in your components instantly.

Auto-Reconnection

Exponential backoff with jitter. Connection drops? The client reconnects automatically. No lost messages -- offline queue with IndexedDB persistence stores messages while disconnected and replays them on reconnect.

End-to-End Encryption

ECDH P-256 key exchange with AES-GCM-256 per connection. Each connection negotiates a unique session key during the handshake (server_ready / client_hello), then all sensitive messages are encrypted end-to-end. HMAC-SHA256 message signing for integrity. Pluggable encryption and token providers via Python protocols.

Message Ordering

Sequence numbers with gap detection and reordering buffer. Messages arrive in order even under high load or network instability. Out-of-order messages are buffered and delivered once the gap fills.

Authentication

JWT-based with HS256, validated in Rust during the WebSocket handshake (zero GIL, 0.01ms decode). Per-connection, per-topic access control. Cookie-based token extraction for seamless browser auth. Fallback to Python auth handler when Rust JWT is not configured.

Health Monitoring

Connection quality scoring (excellent / good / fair / poor), latency tracking, jitter analysis, packet loss detection. Your UI knows when the connection is degraded and can react accordingly.

Scaling

Redis pub/sub for multi-process fan-out. Run multiple server workers behind a load balancer. Clients get messages from any worker. Fire-and-forget delivery with sub-millisecond latency.

Rust Performance

Compression, sequencing, filtering, rate limiting, and the WebSocket server itself are implemented in Rust via PyO3. Python API stays the same. Rust accelerates transparently.


Full Feature List

Server (Python + Rust)

Feature Description
Drain Mode Batch-polling inbound events from Rust. One GIL acquisition per batch (up to 256 messages) instead of per-message Python callbacks. Condvar-based wakeup for zero busy-wait.
Write Coalescing Outbound pipeline: feed() + batch try_recv() + single flush(). Reduces syscalls under load by coalescing multiple messages into one write.
Ping/Pong in Rust Heartbeat handled entirely in Rust with zero Python round-trips. Configurable intervals. TCP_NODELAY on accept for minimal latency.
5-Level Priority Queue CRITICAL(10), HIGH(8), NORMAL(5), LOW(3), BACKGROUND(1). Smart dropping under backpressure: lower-priority messages are dropped first. Batch dequeue ordered by priority.
Dead Letter Queue Redis-backed DLQ for failed messages. 7-day TTL, 1000-message cap per channel. Manual replay via replay_dlq_message(). Prometheus metrics for DLQ size and replay count.
MongoDB-like Filters 14 operators: $eq, $ne, $gt, $lt, $gte, $lte, $in, $nin, $regex, $exists, $contains, $startswith, $endswith. Logical: $and, $or. Dot-notation for nested fields (payload.price). Compiled regex cache.
Event Sequencer Monotonic sequence numbers with AHashSet dedup. Size-based and age-based eviction. Gap detection on both server and client.
Compression Flate2 zlib with configurable levels (1-9). Adaptive threshold -- only compress when payload exceeds size limit. Binary mode via msgpack (rmp-serde) for 30% smaller payloads.
Rate Limiter Atomic token-bucket rate limiter in Rust. Per-connection rate enforcement. 100K tokens capacity, 10K tokens/sec refill.
Message Deduplication AHashSet-backed dedup with bounded queue. Prevents duplicate delivery across reconnections and Redis fan-out.
Wire Envelope Protocol v1: {t, id, ts, seq, p, v}. Generic payload extraction with automatic type conversion (UUID, datetime, Enum, bytes to JSON-safe primitives). Latency tracking (latency_ms field).
Snapshot Provider Protocol for initial state delivery. Implement get_snapshot(user_id, topics) and clients receive current state immediately on subscribe -- no waiting for the next publish cycle.
Circuit Breaker Three-state machine (CLOSED / OPEN / HALF_OPEN). Sliding-window failure tracking. Automatic recovery probes. Prevents cascade failures when downstream services are unhealthy.
Message Categories S (snapshot), U (update), WSE (system). Category prefixing for client-side routing and filtering.
PubSub Bus Redis pub/sub with PSUBSCRIBE pattern matching. orjson fast-path serialization. Custom JSON encoder for UUID, datetime, Decimal. Non-blocking handler invocation.
Pluggable Security EncryptionProvider and TokenProvider protocols. Built-in: AES-GCM-256 with ECDH P-256 key exchange, HMAC-SHA256 signing, selective message signing. Rust-accelerated crypto (SHA-256, HMAC, AES-GCM, ECDH).
Rust JWT Authentication HS256 JWT validation in Rust during the WebSocket handshake. Zero GIL acquisition on the connection critical path. 0.01ms decode (85x faster than Python). Cookie extraction and server_ready sent from Rust before Python runs.
Lock-Free Server Queries get_connection_count() uses AtomicUsize — zero GIL, zero blocking, safe to call from async Python handlers. No channel round-trip to the tokio runtime.
Inbound MsgPack Binary frames from msgpack clients are parsed in Rust via rmpv. Python receives pre-parsed dicts regardless of wire format. Zero Python overhead for msgpack connections.
Connection Metrics Prometheus-compatible stubs for: messages sent/received, publish latency, DLQ size, handler errors, circuit breaker state. Drop-in Prometheus integration or use the built-in stubs.

Client (React + TypeScript)

Feature Description
useWSE Hook Single React hook for the entire WebSocket lifecycle. Accepts topics, endpoints, auth tokens. Returns isConnected, connectionHealth, connection controls.
Connection Pool Multi-endpoint support with health-scored failover. Three load-balancing strategies: weighted-random, least-connections, round-robin. Automatic health checks with latency tracking.
Adaptive Quality Manager Adjusts React Query defaults based on connection quality. Excellent: staleTime: Infinity (pure WebSocket). Poor: aggressive polling fallback. Dispatches wse:quality-change events. Optional QueryClient integration.
Offline Queue IndexedDB-backed persistent queue. Messages are stored when disconnected and replayed on reconnect, ordered by priority. Configurable max size and TTL.
Network Monitor Real-time latency, jitter, and packet-loss analysis. Determines connection quality (excellent / good / fair / poor). Generates diagnostic suggestions.
Event Sequencer Client-side sequence validation with gap detection. Out-of-order buffer for reordering. Duplicate detection via seen-ID window with age-based eviction.
Circuit Breaker Client-side circuit breaker for connection attempts. Prevents reconnection storms when the server is down. Configurable failure threshold and recovery timeout.
Compression + msgpack Client-side decompression (pako zlib) and msgpack decoding. Automatic detection of binary vs JSON frames.
Zustand Stores useWSEStore for connection state, latency history, diagnostics. useMessageQueueStore for message buffering with priority. Lightweight, no boilerplate.
Rate Limiter Client-side token-bucket rate limiter for outbound messages. Prevents flooding the server.
Security Manager Client-side HMAC verification and optional decryption. Validates message integrity before dispatching to handlers.

Client (Python)

Feature Description
Async + Sync API AsyncWSEClient with async context manager and async iterator. SyncWSEClient wrapper for threaded/synchronous code.
Connection Manager Auto-reconnection with 4 strategies (exponential, linear, fibonacci, adaptive). Jitter, configurable max attempts. Heartbeat with PING/PONG.
Connection Pool Multi-endpoint support with health scoring. Weighted-random, least-connections, round-robin load balancing.
Circuit Breaker Three-state machine (CLOSED / OPEN / HALF_OPEN). Prevents reconnection storms.
Rate Limiter Client-side token-bucket rate limiter for outbound messages.
Event Sequencer Duplicate detection (10K ID window) and out-of-order reordering buffer.
Network Monitor Latency, jitter, packet loss analysis. Connection quality scoring.
Security ECDH P-256 key exchange, AES-GCM-256 encryption, HMAC-SHA256 signing. Wire-compatible with server and TypeScript client.
Compression + msgpack Zlib decompression and msgpack decoding. Automatic binary frame detection.

Performance

Rust-accelerated engine via PyO3. Benchmarked on localhost.

Apple M2 (8 cores)

Metric Single Client 10 Workers
Sustained throughput (JSON) 113,000 msg/s 356,000 msg/s
Sustained throughput (MsgPack) 116,000 msg/s 345,000 msg/s
Burst throughput (JSON) 106,000 msg/s 488,000 msg/s
Connection latency 0.53 ms median 2.20 ms median
Ping RTT 0.09 ms median 0.18 ms median
64KB messages 43K msg/s (2.7 GB/s) 164K msg/s (10.2 GB/s)

AMD EPYC 7502P (64 cores, 128 GB)

Metric 64 Workers 128 Workers
Sustained throughput (JSON) 2,045,000 msg/s 2,013,000 msg/s
Sustained throughput (MsgPack) 2,072,000 msg/s 2,041,000 msg/s
Burst throughput (JSON) 1,557,000 msg/s 1,836,000 msg/s
Connection latency 2.60 ms median 2.96 ms median
Ping RTT 0.26 ms median 0.41 ms median
64KB messages 256K msg/s (16.0 GB/s) 238K msg/s (14.9 GB/s)

See BENCHMARKS.md for full results and methodology.


Use Cases

WSE works for any real-time communication between frontend and backend:

  • Live dashboards -- stock prices, sensor data, analytics, monitoring panels
  • Notifications -- order updates, alerts, system events pushed to the browser
  • Collaborative apps -- shared cursors, document editing, whiteboarding
  • Chat and messaging -- group chats, DMs, typing indicators, read receipts
  • IoT and telemetry -- device status, real-time metrics, command and control
  • Gaming -- game state sync, leaderboards, matchmaking updates

Installation

# Server (Python) -- includes prebuilt Rust engine
pip install wse-server

# Client (React/TypeScript)
npm install wse-client

# Client (Python) -- pure Python, no Rust required
pip install wse-client

Server: prebuilt wheels for Linux (x86_64, aarch64), macOS (Intel, Apple Silicon), and Windows. Python 3.12+ (ABI3 stable -- one wheel per platform).

Python client: pure Python, Python 3.11+. Optional extras: pip install wse-client[crypto] for encryption, pip install wse-client[all] for everything.


Architecture

React Client (TypeScript)       Python Client              Server (Python + Rust)
========================        ========================   ========================

useWSE hook                     AsyncWSEClient             FastAPI Router (/wse)
    |                               |                        -- OR --
    v                               v                      RustWSEServer (:5006)
ConnectionPool                  ConnectionPool                  |
    |  (multi-endpoint,             |  (multi-endpoint,         v
    |   health scoring)             |   health scoring)   Rust Engine (PyO3)
    v                               v                          |
ConnectionManager               ConnectionManager              v
    |  (auto-reconnect,             |  (auto-reconnect,   EventTransformer
    |   circuit breaker)            |   circuit breaker)       |
    v                               v                          v
MessageProcessor                MessageCodec              PriorityQueue
    |  (decompress, verify,         |  (decompress,            |
    |   sequence, dispatch)         |   sequence, dedup)       v
    v                               v                     Sequencer + Dedup
AdaptiveQualityManager          NetworkMonitor                 |
    |                               |  (quality scoring,       v
    v                               |   latency, jitter)  Compression + Rate Limiter
Zustand Store                       v                          |
    |                           Event handlers /               v
    v                           async iterator            PubSub Bus (Redis)
React Components                                               |
                                                               v
                                                          Dead Letter Queue

Wire format (v1):

{
  "v": 1,
  "id": "019503a1-...",
  "t": "price_update",
  "ts": "2026-01-15T10:30:00Z",
  "seq": 42,
  "p": { "symbol": "AAPL", "price": 187.42 }
}

Packages

Package Registry Language Install
wse-server PyPI Python + Rust pip install wse-server
wse-client npm TypeScript + React npm install wse-client
wse-client PyPI Python pip install wse-client

All packages are standalone. No shared dependencies between server and clients.


Documentation

Document Description
Protocol Spec Wire format, versioning, encryption
Architecture System design, data flow
Benchmarks Methodology, results, comparisons
Security Model Encryption, auth, threat model
Integration Guide FastAPI setup, Redis, deployment

Technology Stack

Component Technology Purpose
Rust engine PyO3 + maturin Compression, sequencing, filtering, rate limiting, WebSocket server
Server framework FastAPI + Starlette ASGI WebSocket handling
Serialization orjson (Rust) Zero-copy JSON
Binary protocol msgpack (rmp-serde) 30% smaller payloads
Encryption AES-GCM-256 + ECDH P-256 (Rust) Per-connection E2E encryption with key exchange
Message signing HMAC-SHA256 (Rust) Per-message integrity verification
Authentication Rust JWT (HS256) Zero-GIL token validation in handshake
Pub/Sub backbone Redis Pub/Sub Multi-process fan-out
Dead Letter Queue Redis Lists Failed message recovery
Client state Zustand Lightweight React store
Client hooks React 18+ useWSE hook with TypeScript
Offline storage IndexedDB Persistent offline queue
Python client websockets + cryptography Async/sync WebSocket client
Build system maturin Rust+Python hybrid wheels

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wse_server-1.3.9.tar.gz (110.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

wse_server-1.3.9-cp312-abi3-win_amd64.whl (1.5 MB view details)

Uploaded CPython 3.12+Windows x86-64

wse_server-1.3.9-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

wse_server-1.3.9-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.4 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

wse_server-1.3.9-cp312-abi3-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

wse_server-1.3.9-cp312-abi3-macosx_10_12_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

Details for the file wse_server-1.3.9.tar.gz.

File metadata

  • Download URL: wse_server-1.3.9.tar.gz
  • Upload date:
  • Size: 110.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wse_server-1.3.9.tar.gz
Algorithm Hash digest
SHA256 2a3bcbd11a157c303f867a0166cfa78b7d73315410ff531eaada973bae21149b
MD5 68a3f5a5192a571214bd53461f2c6770
BLAKE2b-256 726ccf30cdb2f6b8f52656cecfa4582b3870405bc7bc2c534db352d050afe662

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9.tar.gz:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wse_server-1.3.9-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: wse_server-1.3.9-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for wse_server-1.3.9-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f05efdcfe4cf9106ac10fcd3e3a403137311c92899c0077151ffdc5fae7b32dd
MD5 1feb469024f579b01d06c26e2a8c8e7f
BLAKE2b-256 1492300fbe2cf2648aeac640231d6d289ab765151bee781340485e79fe42f63f

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9-cp312-abi3-win_amd64.whl:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wse_server-1.3.9-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wse_server-1.3.9-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3ec30016d84ed4a51c1419d23b86be7239a2ba987699a26c915d9ad868aaece7
MD5 8bd1b81cc88c08a2e95baf169aee1496
BLAKE2b-256 49f5d82f6b0cd271c00f55b175fbc4acbeb81510a8df97dcac5a5b433f59aef4

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wse_server-1.3.9-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for wse_server-1.3.9-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 f82393ec35a994f6012c18e985685826583e455f98240f109d7e5817a60a42bc
MD5 0e05be02e43ca2e06a93c57b5f3d3d02
BLAKE2b-256 251357907775e2894ccca6ca103559dac33f29b22b61a84c3dbd7f902b51f3a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wse_server-1.3.9-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wse_server-1.3.9-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 32680b92d235c0a8f1c89ae1933ac50c853c7aebc16aa7e1de5fa06b0cec3344
MD5 97c8c634407d540f821ee3ec090c7a1f
BLAKE2b-256 45ef5277d6687932be9f51e613eb5714e1775de266dc54b8589c35b78737457c

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file wse_server-1.3.9-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for wse_server-1.3.9-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 53159d9973526b3c02790496bd297a32f75deaaf348e5c89fd1f6cbc6193cf14
MD5 151b23508b3ce187eaaccedd600a07ce
BLAKE2b-256 a8a50514606431fdca6701b93b669b845c6af8b50160a3acb9bb7a7f6add338b

See more details on using hashes here.

Provenance

The following attestation bundles were made for wse_server-1.3.9-cp312-abi3-macosx_10_12_x86_64.whl:

Publisher: release.yml on silvermpx/wse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page