返回顶部
w

websocket-patterns

Deep WebSocket/SSE workflow—handshake and auth, session lifecycle, heartbeats, ordering, backpressure, scaling, and observability. Use when building realtime dashboards, chat, collaborative editing, or live notifications.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.0.0
安全检测
已通过
104
下载量
0
收藏
概述
安装方式
版本历史

websocket-patterns

# WebSocket Patterns (Deep Workflow) Realtime connections add **stateful** complexity: **who is connected**, **what order** messages arrive, and **what happens** when links flap. Design for **at-least-once** delivery, **explicit** heartbeats, and **horizontal** scaling early. ## When to Offer This Workflow **Trigger conditions:** - Replacing polling with **WS** or **SSE** - Auth on connect; token refresh mid-session - **Fan-out** to many subscribers; **presence** and **typing** indicators - Sticky sessions, load balancer timeouts, **reconnect storms** **Initial offer:** Use **six stages**: (1) choose transport, (2) connection & auth, (3) protocol & messages, (4) reliability & ordering, (5) scale & ops, (6) security & abuse). Confirm **browser vs server** clients and **proxies** (nginx, ALB, Cloudflare). --- ## Stage 1: Choose Transport **Goal:** **WebSocket** vs **SSE** vs **long polling**—right tool per direction. ### Heuristics - **Bidirectional**, low latency, binary payloads → **WebSocket** - **Server → client** **one-way** streams, HTTP-friendly infra → **SSE** - **Fire-and-forget** notifications with **simple** infra → consider **push** services first ### Caveats - **Corporate proxies** historically hurt WS—**test** environments; **WSS** mandatory - **HTTP/3** **QUIC** stacks differ—validate intermediaries **Exit condition:** **Transport choice** documented with **why not** alternatives. --- ## Stage 2: Connection & Auth **Goal:** **Authenticated** sockets without **long-lived** secrets in query strings when avoidable. ### Patterns - **JWT** in **Sec-WebSocket-Protocol** or **first message** after connect—**prefer** short-lived tokens + **refresh** flow - **Cookie** sessions with **CSRF** considerations on **same-site** policies - **Re-auth** before token expiry; **graceful** close with **code** and **reason** ### Authorization - **Subscribe** to **topics** only after **server-side** check—**never** trust client channel names alone **Exit condition:** **Auth** diagram: issue token → connect → **authorize** subscriptions. --- ## Stage 3: Protocol & Messages **Goal:** **Versioned** message schema; **predictable** errors. ### Design - **Envelope**: `{ type, id, ts, payload }`; **correlation** ids for RPC-style - **Version** negotiation on connect or **feature** flags in hello message - **Binary** vs JSON—**protobuf/msgpack** for bandwidth; **JSON** for debuggability early ### Heartbeats - **Ping/pong** or **application-level** heartbeat at **interval < proxy timeout** (often **30–60s**) - **Idle** detection and **clean** disconnect **Exit condition:** **Protocol doc** + **example** session transcript. --- ## Stage 4: Reliability & Ordering **Goal:** Define **delivery semantics**—usually **at-least-once** over TCP; **ordering** per channel. ### Practices - **Idempotent** message handlers; **dedupe** by **message id** when retries exist - **Per-user** sequence numbers if **strict** order matters - **Buffer** limits: **drop**, **close**, or **apply backpressure** policy ### Reconnect - **Exponential backoff** + **jitter** to prevent **thundering herd** - **Resume** from **last seen seq** if **missed messages** are unacceptable—**persist** or **snapshot** **Exit condition:** **Reconnect** story documented; **storm** mitigation tested. --- ## Stage 5: Scale & Operations **Goal:** **Many connections** across **many** nodes—**affinity** and **pub/sub** backbone. ### Architecture - **Sticky sessions** or **shared** **pub/sub** (Redis, NATS, Kafka) for cross-node fan-out - **Shard** connection maps; **avoid** **single** giant in-memory map on one box ### Observability - **Metrics**: active connections, msg/sec, **queue depth**, **disconnect** reasons - **Tracing**: connect → subscribe → **first message** latency ### Load shedding - **Max** connections per IP/user; **rate limit** connection attempts **Exit condition:** **Capacity** model: connections per node × **message** **fan-out** cost. --- ## Stage 6: Security & Abuse **Goal:** **Minimize** attack surface on **long-lived** pipes. ### Controls - **WSS** everywhere; **validate** **Origin** where applicable - **Payload size** limits; **compression** **bomb** awareness - **AuthZ** on every **subscription**; **audit** **admin** actions ### Abuse - **Spam** detection; **kick/ban** flows; **circuit breakers** on **misbehaving** clients --- ## Final Review Checklist - [ ] Transport choice justified (WS/SSE/etc.) - [ ] AuthN/Z on connect and per-channel - [ ] Heartbeats aligned with proxy/LB timeouts - [ ] Delivery/idempotency/reconnect semantics explicit - [ ] Horizontal scale path + observability + abuse controls ## Tips for Effective Guidance - **ALB idle timeout** vs **heartbeat**—classic production bug; call it out. - When user says “real-time,” ask **latency target** and **ordering** needs. - **SSE** is simpler—don’t default to WS for **one-way** feeds. ## Handling Deviations - **Edge runtimes** (Workers): **different** connection limits and **duration**—validate platform. - **Mobile**: **background** **suspension**—**push** notifications may complement WS.

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 websocket-patterns-1776030860 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 websocket-patterns-1776030860 技能

通过命令行安装

skillhub install websocket-patterns-1776030860

下载 Zip 包

⬇ 下载 websocket-patterns v1.0.0

文件大小: 3.22 KB | 发布时间: 2026-4-13 12:33

v1.0.0 最新 2026-4-13 12:33
Initial release with comprehensive real-time WebSocket/SSE workflow guide.

- Outlines six core stages: transport choice, connection/auth, protocol/messages, reliability/ordering, scale/operations, and security/abuse.
- Includes actionable patterns for handshake, token refresh, presence, heartbeats, reconnect storms, and load balancing.
- Provides checklists, design heuristics, and guidance for delivery guarantees, ordering, backpressure, and horizontal scaling.
- Contains observability, abuse protection, and operational tips for production environments.
- Designed for use cases like live dashboards, chat, collaborative editing, or notification systems.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部