websocket
Production-grade WebSocket patterns for Python (FastAPI/Starlette) with connection management, rooms, and message protocols
What this skill does
# WebSocket Skill
Production-grade WebSocket patterns for real-time communication in Python applications.
## When to Use This Skill
Use this skill when:
- Building real-time features (chat, notifications, live updates)
- Implementing bidirectional client-server communication
- Creating collaborative features (multi-user editing, presence)
- Building dashboards with live data feeds
- Implementing game servers or real-time applications
## Core Mental Model
A WebSocket connection is fundamentally different from HTTP:
```
HTTP: Request → Response → Done
WebSocket: Connect → Accept → [Receive Loop] → Disconnect → Cleanup
```
The **receive loop** is unavoidable because:
1. The connection is long-lived - something must continuously listen
2. Messages arrive asynchronously from the client
3. The loop defines the connection's lifetime boundary
4. Even when frameworks abstract it, a loop exists underneath
## Architecture Overview
```
┌─────────────────────────────────────────────────────────────┐
│ WebSocket System │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────┐ ┌──────────────────────────────────┐ │
│ │ Client 1 │───▶│ │ │
│ └──────────────┘ │ │ │
│ │ Connection Manager │ │
│ ┌──────────────┐ │ ┌─────────────────────────────┐ │ │
│ │ Client 2 │───▶│ │ connections: Set[WebSocket] │ │ │
│ └──────────────┘ │ │ rooms: Dict[str, Set[WS]] │ │ │
│ │ └─────────────────────────────┘ │ │
│ ┌──────────────┐ │ │ │
│ │ Client N │───▶│ connect() → track │ │
│ └──────────────┘ │ disconnect() → remove │ │
│ │ broadcast() → fan-out │ │
│ │ send_to_room() → targeted │ │
│ └──────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
```
## File Structure
```
app/
├── websocket/
│ ├── __init__.py
│ ├── manager.py # ConnectionManager class
│ ├── handlers.py # Message handlers by type
│ ├── models.py # Message schemas (Pydantic)
│ └── router.py # WebSocket endpoint
└── main.py # Mount WebSocket router
```
## Core Principles
### 1. Explicit Connection Lifecycle
Connections must be:
- **Accepted explicitly** - Never assume connection success
- **Tracked centrally** - Single source of truth
- **Removed on disconnect** - Eager cleanup prevents leaks
- **Silent failures are worse than explicit disconnects**
### 2. Centralized Connection Management
```python
# Connection state lives in ONE place
class ConnectionManager:
def __init__(self):
self.active_connections: set[WebSocket] = set()
async def connect(self, websocket: WebSocket) -> None:
await websocket.accept()
self.active_connections.add(websocket)
def disconnect(self, websocket: WebSocket) -> None:
self.active_connections.discard(websocket)
```
Business logic must NEVER handle socket tracking directly.
### 3. Receive Loop Responsibility
The receive loop:
- Defines connection lifetime
- Is the only entry point for client messages
- Must handle disconnection gracefully
```python
async def websocket_endpoint(websocket: WebSocket):
await manager.connect(websocket)
try:
while True: # THE LOOP
data = await websocket.receive_text()
# validate → dispatch → respond
except WebSocketDisconnect:
pass # Expected, not exceptional
finally:
manager.disconnect(websocket) # Always cleanup
```
### 4. Message Contract Discipline
Every message must be:
- **Structured**: JSON, not raw strings
- **Typed**: `{"type": "chat", "payload": {...}}`
- **Intent-declaring**: The `type` field routes to handlers
```python
class WebSocketMessage(BaseModel):
type: str
payload: dict = {}
```
### 5. Defensive Failure Handling
- Disconnections happen constantly (normal, not exceptional)
- Writes can fail even after reads succeed
- Dead connections must be cleaned eagerly
- One failure must not cascade to others
## Extended Concepts
### Rooms / Channels
A room is a **logical routing layer**, not a socket feature:
```python
class ConnectionManager:
def __init__(self):
self.connections: set[WebSocket] = set()
self.rooms: dict[str, set[WebSocket]] = defaultdict(set)
def join_room(self, websocket: WebSocket, room_id: str) -> None:
self.rooms[room_id].add(websocket)
def leave_room(self, websocket: WebSocket, room_id: str) -> None:
self.rooms[room_id].discard(websocket)
async def broadcast_to_room(self, room_id: str, message: str) -> None:
for connection in self.rooms[room_id]:
await connection.send_text(message)
```
Key insights:
- Connection belongs to exactly 1 manager, but 0+ rooms
- Room membership is explicit (join/leave operations)
- Leaving a room ≠ disconnecting (orthogonal concerns)
### Performance Mental Model
Performance is about **control**, not speed:
| Cost Center | Why It Matters |
|-------------|----------------|
| Fan-out O(n) | Broadcast to 1000 clients = 1000 sends |
| Serialization | JSON encoding per message adds up |
| Slow clients | One blocked send() can stall others |
Safety principles:
1. Never block the receive loop with slow operations
2. Writes must not delay reads
3. One slow client must not affect others
## Generation Order
When implementing WebSocket features:
1. **Define message types** - What messages will be exchanged?
2. **Create ConnectionManager** - Central state management
3. **Implement receive loop** - WebSocket endpoint
4. **Add message handlers** - Route by message type
5. **Add rooms (if needed)** - Logical grouping
6. **Add error handling** - Defensive cleanup
## Quick Reference
```python
# Accept connection
await websocket.accept()
# Receive message
data = await websocket.receive_text()
data = await websocket.receive_json()
# Send message
await websocket.send_text(message)
await websocket.send_json(data)
# Close connection
await websocket.close(code=1000)
# Handle disconnection
from starlette.websockets import WebSocketDisconnect
```
## What "Production-Grade" Means
**Not:**
- Feature-rich
- Horizontally scalable (yet)
- Prematurely optimized
**Yes:**
- Predictable behavior under all conditions
- Clear separation: connection management | business logic | routing
- Safe defaults (cleanup on failure, structured messages)
- Clear upgrade paths to rooms, scaling, tuning
## Explicit Non-Goals
These are intentionally excluded to keep the core pattern clean:
- Authentication / authorization (handle before WebSocket upgrade)
- Persistence (message history is a separate concern)
- Distributed state (requires Redis/pubsub, out of scope)
- Message ordering guarantees across processes
## References
See the `references/` directory for:
- `connection-manager-pattern.md` - Centralized connection tracking
- `receive-loop-pattern.md` - Message loop structure and lifetime
- `message-protocol-pattern.md` - JSON message contracts
- `rooms-pattern.md` - Logical routing and channels
- `error-handling-pattern.md` - Defensive failure handling
Related in Backend & APIs
jfrog
IncludedInteract with the JFrog Platform via the JFrog CLI and REST/GraphQL APIs. Use this skill when the user wants to manage Artifactory repositories, upload or download artifacts, manage builds, configure permissions, manage users and groups, work with access tokens, configure JFrog CLI servers, search artifacts, manage properties, set up replication, manage JFrog Projects, run security audits or scans, look up CVE details, query exposures scan results from JFrog Advanced Security, manage release bundles and lifecycle operations, aggregate or export platform data, or perform any JFrog Platform administration task. Also use when the user mentions jf, jfrog, artifactory, xray, distribution, evidence, apptrust, onemodel, graphql, workers, mission control, curation, advanced security, exposures, or any JFrog product name.
cupynumeric-migration-readiness
IncludedPre-migration readiness assessor for porting NumPy to cuPyNumeric. Use BEFORE substantial porting work begins when the user asks whether code will scale on GPU, whether they should migrate to cuPyNumeric, which NumPy patterns transfer cleanly, what must be refactored before porting, or mentions pre-port assessment, scaling analysis, or refactor planning. Inspect the user's source code, look up NumPy usage, cross-reference the cuPyNumeric API support manifest, and distinguish distributed-scaling-friendly patterns from blockers such as unsupported APIs, scalar synchronization, host round-trips, Python/object-heavy control flow, shape/data-dependent branching, and in-place mutation hazards. Produce a verdict of READY, LIGHT REFACTOR, SIGNIFICANT REFACTOR, or NOT RECOMMENDED, with concrete refactor pointers.
alibabacloud-data-agent-skill
IncludedInvoke Alibaba Cloud Apsara Data Agent for Analytics via CLI to perform natural language-driven data analysis on enterprise databases. Data Agent for Analytics is an intelligent data analysis agent developed by Alibaba Cloud Database team for enterprise users. It automatically completes requirement analysis, data understanding, analysis insights, and report generation based on natural language descriptions. This tool supports: discovering data resources (instances/databases/tables) managed in DMS, initiating query or deep analysis sessions, real-time progress tracking, and retrieving analysis conclusions and generated reports. Use this Skill when users need to query databases, analyze data trends, generate data reports, ask questions in natural language, or mention "Data Agent", "data analysis", "database query", "SQL analysis", "data insights".
token-optimizer
IncludedReduce OpenClaw token usage and API costs through smart model routing, heartbeat optimization, budget tracking, and native 2026.2.15 features (session pruning, bootstrap size limits, cache TTL alignment). Use when token costs are high, API rate limits are being hit, or hosting multiple agents at scale. The 4 executable scripts (context_optimizer, model_router, heartbeat_optimizer, token_tracker) are local-only — no network requests, no subprocess calls, no system modifications. Reference files (PROVIDERS.md, config-patches.json) document optional multi-provider strategies that require external API keys and network access if you choose to use them. See SECURITY.md for full breakdown.
resend-cli
IncludedUse this skill when the task is specifically about operating Resend from an AI agent, terminal session, or CI job via the official resend CLI: installing/authenticating the CLI, sending/listing/updating/cancelling emails, batch sends, domains and DNS, webhooks and local listeners, inbound receiving, contacts, topics, segments, broadcasts, templates, API keys, profiles, or debugging Resend CLI/API failures. Trigger on mentions of Resend CLI, `resend`, `resend doctor`, `resend emails send`, `resend domains`, `resend webhooks listen`, `resend emails receiving`, or agent-friendly terminal automation.
alibabacloud-odps-maxframe-coding
IncludedUse this skill for MaxFrame SDK development and documentation navigation on Alibaba Cloud MaxCompute (ODPS). Helps answer MaxFrame API, concept, official example, and supported pandas API questions; create data processing programs; read/write MaxCompute tables; debug jobs (remote or local); and build custom DPE runtime images. Trigger when users mention MaxFrame, MaxCompute with MaxFrame, ODPS table processing, DPE runtime, MaxFrame docs/examples, DataFrame/Tensor operations, or GPU runtime setup. Works for both English and Chinese queries about Alibaba Cloud data processing with MaxFrame.