用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/mikailustuner/OmniRule --skill websocket-patterns命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
基于 SOC 职业分类
| name | websocket-patterns |
| description | WebSockets: Connection management, reconnection, rooms, and real-time communication. |
| triggers | {"keywords":["WebSocket","ws","socket","connection","reconnect","heartbeat","room","namespace"]} |
| auto_load_when | Implementing WebSocket communication |
| agent | infra-specialist |
| tools | ["Read","Write","Bash"] |
Focus: Real-time bidirectional communication
Connection states:
├── Connecting
│ └── Handshake in progress
│ └── After: HTTP upgrade request
│ └── Timeout: 10-30 seconds
│
├── Connected
│ └── Full-duplex channel open
│ └── Keep-alive: ping/pong every 30s
│ └── Bi-directional messaging
│
├── Closing
│ └── Graceful shutdown initiated
│ └── Code 1000 (normal) or error codes
│ └── Clean-up: release resources
│
└── Disconnected
└── Connection lost
└── Trigger reconnection
When reconnect:
├── Immediate retry
└── First attempt after disconnect
└── 0-1 second delay
│
├── Exponential backoff
└── Delay: 1s, 2s, 4s, 8s, 30s (max)
└── Jitter: random +/- 1 second
└── Reset on successful connection
│
├── Max retries
└── After 5-10 failures, stop
└── Notify user: "connection lost"
└── Offer manual reconnect button
│
└── Always include
└── Random jitter (prevent thundering herd)
└── Backoff on server errors
└── Immediate retry on network change
When to use rooms:
├── User-specific
│ └── Private messages
│ └── Per-user notification stream
│ └── Join room: user:{userId}
│
├── Group-based
│ └── Team chat, group conversations
│ └── Join room: team:{teamId}
│ └── Broadcast to group
│
├── Broadcast
│ └── All connected clients
│ └── Announcements, system updates
│ └── No room needed, send to all
│
└── Topic-based (pub/sub)
└── Multiple interests per user
└── Subscribe: notifications, updates, alerts
└── Pattern: MQTT-style routing
Message types:
├── Client → Server
│ ├── ACTION: user performed action
│ ├── SUBSCRIBE: join room/channel
│ ├── UNSUBSCRIBE: leave room/channel
│ └── PING: keep-alive (optional)
│
└── Server → Client
├── EVENT: real-time update
├── BROADCAST: room-wide message
├── ACK: confirm message received
└── PONG: keep-alive response
Protocol design:
├── Include message type/enum
├── Include payload (JSON)
├── Include timestamp (server-side)
├── Include correlation ID (request/response)
└── Include message ID (for ACKs)
Single server (start here):
├── In-memory connection storage
├── Works for < 10k concurrent connections
└── Simple, no external dependencies
Redis pub/sub (horizontal scaling):
├── Store connections in Redis
├── Route messages through Redis
├── Can scale to millions
├── Add: Redis adapter for Socket.io
└── Trade-off: latency increase
WebSocket gateway (production):
├── Dedicated WS servers
├── Route via load balancer
├── Sticky sessions required
└── Shared state via Redis
❌ No reconnection logic — broken connection = dead client
✅ Exponential backoff reconnect with jitter
❌ Sending full state on every update
✅ Send diffs/patches; client reconciles
❌ No authentication on WebSocket upgrade
✅ Validate JWT/cookie on HTTP upgrade handshake
❌ Unlimited connections per user
✅ Enforce max connections per user; close old on new connect
❌ Ignoring WebSocket close codes
✅ Handle 1000 (normal), 1001 (going away), 4xxx (app errors)
| Scenario | Solution | Note |
|---|---|---|
| Reconnect | Exponential backoff | 1s, 2s, 4s, 8s... max 60s |
| Auth | Token in query or upgrade header | Not in URL for prod |
| Rooms | Map<roomId, Set> | Server-side routing |
| Broadcast | Iterate room sockets | Or Redis Pub/Sub for multi-node |
| Heartbeat | ping/pong interval | 30s; close if no pong |
| Compression | permessage-deflate | Header negotiation |