JetSocket¶
Production-grade, resilient WebSocket library for Python
JetSocket provides a high-performance, memory-efficient WebSocket client with:
- Cython-optimized core for frame parsing and compression
- Automatic reconnection with exponential backoff and jitter
- Heartbeat management (ping/pong)
- Message buffering with replay-on-reconnect
- Both async and sync APIs without thread explosion
- Full type safety with generics and Pydantic support
Quick Example¶
Installation¶
Why JetSocket?¶
| Feature | websockets | websocket-client | aiohttp | JetSocket |
|---|---|---|---|---|
| Auto-Reconnect | ||||
| Exponential Backoff | ||||
| Heartbeat Management | ||||
| Message Buffer | ||||
| Memory Efficient | ||||
| Async + Sync | sync only | async only | ||
| Type Safe | partial | partial |
Features¶
Automatic Reconnection¶
JetSocket handles disconnections gracefully with configurable exponential backoff:
from jetsocket import WebSocket, BackoffConfig
ws = WebSocket(
"wss://stream.example.com/ws",
reconnect=True,
backoff=BackoffConfig(
base=1.0, # Start with 1s delay
cap=60.0, # Max 60s between retries
jitter=True, # Randomize to avoid thundering herd
),
)
Heartbeat Management¶
Never worry about idle disconnections again:
from jetsocket import WebSocket, HeartbeatConfig
ws = WebSocket(
"wss://stream.example.com/ws",
heartbeat=HeartbeatConfig(
interval=20.0, # Send ping every 20s
timeout=10.0, # Wait 10s for pong
),
)
Connection Pooling¶
Manage multiple connections efficiently:
from jetsocket import ConnectionPool, ConnectionPoolConfig
config = ConnectionPoolConfig(max_connections=10)
async with ConnectionPool(config, base_uri="wss://stream.example.com") as pool:
async with pool.acquire("/ws/btcusdt") as conn:
await conn.send({"subscribe": "trades"})
async for msg in conn:
process(msg)
Multiplexing¶
Multiple subscriptions over a single connection:
from jetsocket import Multiplex
async with Multiplex(
"wss://stream.example.com/ws",
channel_key="stream",
subscribe_msg=lambda ch: {"method": "SUBSCRIBE", "params": [ch]},
) as mux:
btc = await mux.subscribe("btcusdt@trade")
eth = await mux.subscribe("ethusdt@trade")
async for trade in btc:
print(f"BTC: {trade}")
Type Safety with Pydantic¶
Full type checking with Pydantic models:
from pydantic import BaseModel
from jetsocket import WebSocket
class TradeMessage(BaseModel):
symbol: str
price: float
quantity: float
async with WebSocket("wss://stream.example.com/ws", message_type=TradeMessage) as ws:
async for trade in ws: # trade is TradeMessage
print(f"{trade.symbol}: ${trade.price:.2f}")
Presets for Common Use Cases¶
Optimized configurations for different scenarios:
from jetsocket.presets import trading, llm_stream, dashboard
# For crypto trading
ws = trading("wss://stream.binance.com/ws")
# For LLM streaming
ws = llm_stream("wss://api.openai.com/v1/realtime")
# For dashboards
ws = dashboard("wss://dashboard.example.com/ws")
What's Next?¶
- Installation - Get JetSocket installed
- Quickstart - Build your first WebSocket client
- User Guide - Deep dive into all features
- API Reference - Full API documentation