build-realtime-channel
Skill9 starsChanged 5 months agoApache-2.0View on GitHub
Builds realtime push channels over WebSocket/SSE — auth-on-connect, heartbeat/zombie eviction, topic subscribe/publish with per-topic authz and presence, sequence-numbered resume for missed-message recovery, client reconnect with backoff+jitter, and a Redis/NATS pub/sub backplane with send-buffer limits for horizontal scale.
In this repository147 other files it gives its agents.
- second-brain/AGENTS.mdAGENTS.md
- second-brain/CLAUDE.mdCLAUDE.md
- agent-tool-mcp-builderSkill
- api-design-reviewSkill
- async-concurrency-correctnessSkill
- audit-accessibility-wcagSkill
- audit-license-complianceSkill
- audit-technical-seoSkill
- auth-jwt-sessionSkill
- author-codemodSkill
- brainstorm-designSkill
- build-audit-loggingSkill
- build-cdc-streaming-pipelineSkill
- build-cli-toolSkill
- build-data-tableSkill
- build-etl-pipelineSkill
- build-form-validationSkill
- build-native-mobile-uiSkill
- build-office-docsSkill
- build-offline-first-syncSkill
- build-react-componentSkill
- build-spreadsheetSkill
- build-vector-searchSkill
- caching-strategySkill
- cicd-pipeline-authorSkill
- cloud-cost-optimizeSkill
- code-commentsSkill
- code-reviewSkill
- compose-local-dev-stackSkill
- configure-bundler-buildSkill
- configure-dns-tlsSkill
- configure-reverse-proxy-lbSkill
- configure-security-headers-cspSkill
- contract-testingSkill
- datetime-timezone-correctnessSkill
- db-migration-safetySkill
- debug-ci-pipeline-failureSkill
- debug-flaky-testsSkill
- debug-frontend-browserSkill
- debug-root-causeSkill
- defend-llm-prompt-injectionSkill
- deliver-webhooksSkill
- dependency-upgradeSkill
- deploy-releaseSkill
- design-api-paginationSkill
- design-authorization-modelSkill
- design-backup-dr-recoverySkill
- design-event-sourcing-cqrsSkill
- design-multi-tenancySkill
- design-protobuf-grpc-serviceSkill
- design-relational-schemaSkill
- design-search-index-infraSkill
- design-state-machineSkill
- design-token-systemSkill
- diff-table-paritySkill
- distributed-locks-leasesSkill
- dockerfile-optimizeSkill
- encrypt-sensitive-dataSkill
- error-messageSkill
- estimate-workSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
3 rules. About 0 are common, 1 is distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 64
If the gap exceeds the buffer, send a reset/snapshot-required signal so the client refetches full state instead of silently missing data.
- Very specific: line 90
Sticky sessions: only needed for handshake-split transports (Socket.IO long-poll→WS upgrade must hit the same node) — set LB affinity or …
- Distinctive: line 101
No jitter on reconnect: All clients backoff on the same schedule and reconnect in lockstep after an outage/deploy — synchronized thundering …
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use
Reach for this skill when the request is about pushing live data to clients over a long-lived connection:
- "Push notifications / chat messages / order updates to the browser in realtime"
- "Build a live dashboard / activity feed / collaborative cursors that updates without polling"
- "Should this be WebSocket, SSE, or long-poll?"
- "Our socket drops messages after a reconnect" / "clients miss updates while disconnected"
- "Connections leak — server FD count climbs and never drops" / "zombie sockets pile up"
- "On deploy every client reconnects at once and melts the box" (thundering herd)
- "Realtime works on one node but breaks behind a load balancer / can't scale out"
NOT this skill:
- Durable server-to-server work queues, retries, dead-letter, exactly-once job processing → message-queue-jobs (this skill is at-most/at-least-once push to clients, not a job system)
- Native mobile push via APNs/FCM, device-token registration, woken-from-killed delivery → implement-push-notifications (OS push to a closed app; this skill is an open in-app socket/stream)
- Client-side cache, refetch, optimistic UI, query invalidation → manage-client-server-state (that's what the client does with pushed data; this is the wire)
- Issuing/validating the token itself, refresh, session rotation → auth-jwt-session (this skill consumes a token on connect, it doesn't mint it)
- Capping how many connects/messages a client may send → rate-limiting
- Races inside your fan-out/handler code (shared mutable state, missing await) → async-concurrency-correctness
- Metrics/tracing/log wiring for the channel → observability-instrument
Steps
Pick the transport by directionality — do not default to WebSocket. Most "realtime" needs are server→client only.
Transport Use when Cost / caveat SSE ( text/event-stream)Server→client only (feeds, notifications, dashboards, token streaming). Default for one-way. One HTTP/1.1 conn per stream (use HTTP/2 to avoid 6-conn cap); auto-reconnect + Last-Event-IDbuilt in; no binaryWebSocket True bidirectional, low-latency, high message rate (chat, presence, games, collaborative editing) Manual heartbeat/reconnect/resume; no auto-resume; proxies/LBs need explicit UpgradesupportLong-poll Fallback only when SSE/WS are blocked (ancient proxy, locked-down corp net) High overhead, ~1 msg per round trip; keep as graceful degradation, not primary Rule: one-way → SSE; bidirectional or >~10 msg/s/client → WebSocket; long-poll only as fallback. Don't hand-roll if a maintained lib fits —
Socket.IO(built-in reconnect+rooms+fallback),Phoenix Channels(presence+backpressure+cluster PubSub out of the box),Centrifugo(standalone server, history/recovery built in),SignalR(.NET). Rawws/SSE only when you need full control and will build lifecycle yourself.Authenticate on connect — never in the query string. A token in
?token=...lands in access logs, proxy logs, andReferer. Validate before upgrading.- WebSocket: pass the token via the
Sec-WebSocket-Protocolsubprotocol header, or require an authenticated cookie (sent automatically on the upgrade), or accept an unauthenticated socket and require anauthframe as the first message within a short deadline (≤5s) or close. - SSE: EventSource can't set headers — use a same-site auth cookie, or a short-lived single-use ticket fetched over a normal authed request then passed once.
- On bad/expired token: WS close code
4401(app range; reserve4403for authz failure), SSE respond401before the stream opens. Re-check token expiry on long-lived sockets; close when it lapses.
// WS upgrade with subprotocol-carried token (client) new WebSocket("wss://api.example.com/ws", ["bearer", token]); // server: read token from Sec-WebSocket-Protocol, verify, then accept (echo the protocol)- WebSocket: pass the token via the
- Run the full connection lifecycle — this is where leaks live.
- Heartbeat: WS — server sends
pingevery 30s, expectspong; if 2 missed (60s), terminate the socket (a half-open TCP conn looks alive to the OS but is dead). SSE — emit a comment line:keep-alive\n\nevery 15–30s so proxies don't idle-close.wsclients that neverpongare zombies; anisAliveflag flipped false on each ping and reset on pong evicts them. - Zombie eviction: sweep on an interval;
socket.terminate()(not.close()) anything that failed the heartbeat. Track open connections in a registry so you can count and reap them. - Graceful drain on deploy: on
SIGTERM, stop accepting new connections, send agoing_awayapp message (so clients reconnect staggered, not instantly), then close with code1001after a grace window. Neverkill -9a live channel node — every client stampedes back at once.
- Heartbeat: WS — server sends
- Model subscriptions as topics with per-topic authz, and add presence. A connection is not a subscription. Let a client subscribe to named topics/channels (
chat:room:42,user:7:notifications) over one socket.- Authorize every subscribe against the current user — a connection authed as user 7 must not subscribe to
user:9:*. Check on subscribe, not just on connect; deny with an error frame, don't silently drop. - Namespace topics so wildcards can't leak (
org:{id}:...). Reject subscribe to topics the user can't read. - Presence: maintain a per-topic set of members in the backplane (Redis
SET/hash keyed by topic, member ={userId, connId}); broadcastjoin/leaveon change. Tie membership to the connection so a dropped socket auto-removes the member (TTL-backed, refreshed by heartbeat — otherwise a crashed client lingers as "online" forever).
- Authorize every subscribe against the current user — a connection authed as user 7 must not subscribe to
Make missed-message recovery explicit with sequence numbers + a resume cursor — decide the delivery guarantee up front. Default to at-least-once + client dedup, not "best effort."
- Stamp every message per-topic with a monotonic
seq(and an eventid). Keep a bounded history buffer per topic (e.g. last N=1000 or last 5 min) in Redis (XADDto a stream, or a capped list). - On (re)subscribe the client sends its last seen
seq(resume_from); server replays buffered events> resume_fromthen switches to live. SSE gets this for free: the browser auto-sendsLast-Event-IDon reconnect — honor it and replay. - If the gap exceeds the buffer, send a
reset/snapshot-required signal so the client refetches full state instead of silently missing data. - Guarantee table — pick one and document it:
Guarantee Mechanism Cost Best-effort (at-most-once) Fire-and-forget, no buffer Drops on any disconnect — only for ephemeral (live cursor pos) At-least-once + dedup seq + history buffer + resume cursor; client drops seq ≤ lastSeenDefault. Bounded buffer mem; client must dedup Exactly-once delivery Don't. Use at-least-once + idempotent client apply True E2E exactly-once is a distributed-systems tax you don't need - Stamp every message per-topic with a monotonic
Reconnect on the client with backoff + jitter, then resubscribe and dedup. A fixed-delay or zero-delay reconnect loop is how one deploy becomes a self-DDoS.
// exponential backoff, full jitter, cap 30s — applies to WS and SSE-with-manual-reconnect let attempt = 0; function reconnect() { const base = Math.min(30000, 1000 * 2 ** attempt++); const delay = Math.random() * base; // full jitter — spreads the herd setTimeout(connect, delay); } // on open: attempt = 0; resubscribe all topics with resume_from=lastSeq[topic]; // drop any replayed event whose seq <= lastSeq[topic] (dedup)Reset the attempt counter on a successful open, resubscribe every topic with its own
resume_from, and dedup replayed events byseq. Stop reconnecting on a fatal close code (4401/4403) — don't hammer a server that rejected your auth.- Scale horizontally with a pub/sub backplane + per-connection backpressure. A second node means a publish on node A must reach a subscriber on node B.
- Stateless + backplane (preferred): each node holds its own sockets; publishes go to Redis Pub/Sub, Redis Streams, or NATS; every node subscribes and fans out to its local sockets for that topic. No sticky sessions needed for WS (the socket stays pinned to one node by TCP anyway); for SSE/long-poll across nodes you still need either a backplane or sticky routing.
- Sticky sessions: only needed for handshake-split transports (Socket.IO long-poll→WS upgrade must hit the same node) — set LB affinity or force
transports: ['websocket']. Prefer stateless+backplane over relying on stickiness. - Backpressure / slow-consumer: a client that reads slower than you write balloons the per-socket send buffer and OOMs the node. Cap it: watch
ws.bufferedAmount(or your lib's queue depth); if it exceeds a threshold (e.g. 1–4 MB), drop the slow consumer (close1013/4408) rather than buffer unboundedly. For SSE, the same applies to the response stream's write backpressure. One slow consumer must never degrade the rest.
- Load + soak test, then observe. Single-connection tests prove nothing about a channel (see Verify). Wire metrics (open conns, msgs/s, send-buffer high-water, reconnect rate, dropped-slow-consumers) via observability-instrument.
Common Errors
- Token in the query string.
?token=...leaks into access/proxy logs andReferer. Use subprotocol header, auth cookie, or a first-frameauthmessage. - No heartbeat → silent half-open sockets. TCP keepalive defaults to ~2h; a dead peer looks connected for hours. App-level ping/pong (30s) + terminate on miss is mandatory.
.close()instead of.terminate()on a zombie.close()waits for a close handshake the dead peer will never send, so the FD lingers. Terminate failed-heartbeat sockets.- Unbounded per-socket send buffer. One slow/paused client grows
bufferedAmountuntil the node OOMs and takes down every connection. Cap buffer; drop the slow consumer. - No jitter on reconnect. All clients backoff on the same schedule and reconnect in lockstep after an outage/deploy — synchronized thundering herd. Add full jitter.
- Instant/zero-delay reconnect loop. A server that closes on every connect gets hammered thousands of times/sec. Always backoff; stop on fatal auth close codes.
- Treating a connection as a subscription / authz only on connect. A long-lived socket can request any topic later; authorize every
subscribeagainst the current user, namespace topics, deny cross-tenant. - No sequence numbers → "messages disappear" after reconnect. Without
seq+ history + resume there's no way to recover the gap; clients silently miss data. Stamp seq, buffer, replay from cursor (orLast-Event-ID). - In-memory subscriptions/presence with >1 node. A publish on node A never reaches node B's subscribers; presence shows half the users. Use a Redis/NATS backplane; back presence with a TTL'd shared store.
- Presence that never clears. Membership tied to a clean disconnect only — a crashed client stays "online" forever. TTL the entry, refresh on heartbeat.
- No graceful drain on deploy. Killing a node drops every socket simultaneously and they all reconnect at once. SIGTERM → stop accepts →
going_away(staggered reconnect) → close1001. - Relying on sticky sessions to "fix" scale. Stickiness papers over a missing backplane and breaks the moment a node dies (all its clients fail over to a node that doesn't know their topics). Make nodes stateless + backplane; use stickiness only for handshake-split transports.
- SSE without keep-alive comments. Idle proxies/LBs close a quiet
text/event-streamafter their timeout. Emit:keep-aliveevery 15–30s.
Verify
- Auth on connect: connect with no/expired/forged token → rejected before the stream opens (WS close
4401, SSE401); token never appears innginx/access logs. Cross-tenantsubscribe→ denied with an error frame, not silently dropped. - Heartbeat + zombie eviction:
tc/iptables-drop a client's traffic (simulate half-open) → server detects via missed pong within ~60s andterminate()s it; server open-connection gauge returns to baseline (no FD leak). Re-run 100× in a loop — count must not climb. - Missed-message recovery: subscribe, record
seq, kill the client, publish 50 events, reconnect withresume_from/Last-Event-ID→ client receives exactly those 50 in order, zero gaps, zero dupes after dedup. Exceed the buffer → client gets areset/snapshot signal (not a silent gap). - Reconnect storm (thundering herd): connect 5–10k clients, restart/kill the node → reconnects spread over the backoff window (full-jitter histogram, not a spike); server stays up; all topics resubscribed. With zero jitter this test must visibly fail (then pass after adding jitter).
- Horizontal fan-out: run ≥2 nodes behind an LB; a subscriber on node B receives a message published to node A → proves the backplane works, not just one box. Kill node A mid-stream → its clients fail over to node B and resume from cursor with no lost messages.
- Slow-consumer isolation: one client stops reading (pause the socket) while others stay live → the slow one is dropped (
bufferedAmountcap hit, close1013/4408); all other clients keep flowing with no latency spike; node memory stays flat. - Graceful drain: send the node
SIGTERMunder load → clients getgoing_awaythen close1001, reconnect staggered to another node, miss zero messages (resume covers the gap). - Load/soak: drive target concurrent connections at peak msg/s with a WS/SSE load tool (
k6ws/SSE,artillery,vegetafor SSE) for ≥30 min → p99 delivery latency within budget; open-conn count, memory, and send-buffer high-water are flat (no leak/creep).
Done = auth-on-connect (no query-string token), heartbeat-driven zombie eviction with flat FD/conn count under churn, sequence-based resume recovers every missed message with no dupes, reconnect uses backoff+jitter, fan-out works across ≥2 nodes via the backplane, slow consumers are dropped without affecting others, and a ≥30-min soak shows no leak.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

