realtime-websocket-patterns

Implement WebSocket, SSE, and long polling features with connection management and presence tracking.

Updated May 16, 2026
One-click install
npx skills add https://github.com/organvm-i-theoria/_agent-ontology --skill realtime-websocket-patterns-organvm-i-theoria
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: realtime-websocket-patterns
Source: https://github.com/organvm-i-theoria/_agent-ontology/tree/main/.agents/skills/realtime-websocket-patterns
Command: npx skills add https://github.com/organvm-i-theoria/_agent-ontology --skill realtime-websocket-patterns-organvm-i-theoria

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires fastapi, pydantic, sse-starlette, redis.

What problem does it solve? Building real-time features like chat, live dashboards, and presence indicators requires choosing the right protocol and handling connection lifecycle, authentication, reconnection, and scaling—details that are easy to get wrong. ## Core Features & Use Cases - Protocol Selection Guidance: Decision matrix for choosing between WebSocket, SSE, long polling, and WebTransport based on directionality and latency needs. - FastAPI WebSocket Patterns: Room-based connection manager, token authentication, structured message protocol with acknowledgments, and heartbeat-based presence tracking. - Scaling & Reliability: Redis Pub/Sub bridge for multi-server broadcasting, client-side reconnection with exponential backoff, and ordered message buffering. - Use Case: Add a live chat room to a FastAPI application where users authenticate via token, see who is online, receive messages in order, and reconnect automatically after network drops. ## Quick Start Ask the agent to implement a WebSocket chat endpoint in FastAPI with room-based broadcasting, token authentication, and presence tracking.

Frequently Asked Questions about realtime-websocket-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a WebSocket chat room in FastAPI?▼

Create a ConnectionManager class that maps room names to sets of WebSocket connections, then accept connections in a websocket endpoint and broadcast received messages to all sockets in the room. Handle WebSocketDisconnect to clean up stale connections.

WebSocket vs SSE: which should I use for real-time updates?▼

Use WebSocket when you need bidirectional communication like chat or collaboration. Use SSE when data only flows server-to-client, such as notifications, dashboards, or feeds—SSE is simpler and auto-reconnects natively.

How do I scale WebSockets across multiple servers with Redis?▼

Use Redis Pub/Sub as a bridge: each server publishes outgoing messages to a Redis channel per room and subscribes to receive messages from other servers, then broadcasts them to its local connections.

How do I authenticate WebSocket connections in FastAPI?▼

Pass a token as a query parameter to the websocket endpoint, verify it before accepting the connection, and close with code WS_1008_POLICY_VIOLATION if invalid. Authenticate once at connection time, not per message.

Why do WebSocket connections drop and how do I handle reconnection?▼

Connections drop due to network changes, idle timeouts, or server restarts. Implement client-side reconnection with exponential backoff capped at 30 seconds, and send server heartbeats every 30 seconds to detect stale connections.