Websocket realtime engineer

From findscripter/everything-skills · View interactive version

Websocket realtime engineer is an AI agent skill from the GitHub repository findscripter/everything-skills. 当构建 WebSocket/Socket.IO 实时双向通信系统时使用;产出带鉴权、房间、重连、Redis 横向扩展的服务端与客户端实现及扩缩容方案;不适用于单向服务器推送(用 SSE)、低频轮询或纯请求-响应场景。触发词:WebSocket、Socket.IO、实时通信、双向消息、pub/sub、在线状态、房间管理。

How to use this skill

Skill file

何时使用

需要服务端与客户端之间持续、低延迟、双向通信时使用。典型场景:聊天/IM、协同编辑、实时通知、在线状态(presence)、多人房间广播、实时仪表盘。

不该用的边界(先选更简单的方案):

判断要点:是否真正需要「客户端也要主动发」+「低延迟」+「长连接」。三者缺一,多半不必上 WebSocket。

步骤

  1. 分析需求 — 估算并发连接数、消息吞吐量、延迟要求,定下单实例连接上限。
  2. 设计架构 — 规划集群、pub/sub、状态存储(presence/房间放外部存储)、故障转移。
  3. 实现 — 搭建带鉴权(握手中间件)、房间、事件处理的服务端 + 带重连和消息缓冲的客户端。
  4. 本地验证 — 用 npx wscat -c ws://localhost:3000 测试;确认缺失/非法 token 被拒、房间 join/leave 事件、消息送达。
  5. 扩展 — 先验证 Redis 连接与 pub/sub 往返再启用 adapter;配置粘性会话(sticky sessions)并跨多实例验证;接入负载均衡。
  6. 监控 — 跟踪连接数、延迟、吞吐、错误率;对连接数突增和错误率阈值加告警。

指令

MUST DO

MUST NOT

示例

服务端(Socket.IO + 鉴权 + 房间 + Redis 横向扩展)

import { createServer } from "http";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";
import jwt from "jsonwebtoken";

const httpServer = createServer();
const io = new Server(httpServer, {
  cors: { origin: process.env.ALLOWED_ORIGIN, credentials: true },
  pingTimeout: 20000,
  pingInterval: 25000,
});

// 鉴权中间件:在连接建立前运行
io.use((socket, next) => {
  const token = socket.handshake.auth.token;
  if (!token) return next(new Error("Authentication required"));
  try {
    socket.data.user = jwt.verify(token, process.env.JWT_SECRET);
    next();
  } catch {
    next(new Error("Invalid token"));
  }
});

// Redis adapter 实现横向扩展
const pubClient = createClient({ url: process.env.REDIS_URL });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);
io.adapter(createAdapter(pubClient, subClient));

io.on("connection", (socket) => {
  const { userId } = socket.data.user;
  pubClient.hSet("presence", userId, socket.id); // presence:标记在线

  socket.on("join-room", (roomId) => {
    socket.join(roomId);
    socket.to(roomId).emit("user-joined", { userId });
  });

  socket.on("message", ({ roomId, text }) => {
    io.to(roomId).emit("message", { userId, text, ts: Date.now() });
  });

  socket.on("disconnect", () => {
    pubClient.hDel("presence", userId); // 清理 presence
  });
});

httpServer.listen(3000);

客户端(指数退避重连 + 断线消息缓冲)

import { io } from "socket.io-client";

const socket = io("wss://api.example.com", {
  auth: { token: getAuthToken() },
  reconnection: true,
  reconnectionAttempts: 10,
  reconnectionDelay: 1000,       // 初始延迟 ms
  reconnectionDelayMax: 30000,   // 上限 30 s
  randomizationFactor: 0.5,      // 抖动,避免惊群
});

let messageQueue = []; // 断线时缓冲

socket.on("connect", () => {
  messageQueue.forEach((msg) => socket.emit("message", msg)); // 重连后冲刷
  messageQueue = [];
});

socket.on("disconnect", (reason) => {
  if (reason === "io server disconnect") socket.connect(); // 服务端主动断开需手动重连
});

socket.on("connect_error", (err) => console.error(err.message));

function sendMessage(roomId, text) {
  const msg = { roomId, text };
  if (socket.connected) socket.emit("message", msg);
  else messageQueue.push(msg); // 缓冲直到重连
}

交付清单(实现 WebSocket 功能时一并给出)

  1. 服务端配置(Socket.IO/ws);
  2. 事件处理(connection、message、disconnect);
  3. 客户端库(连接、事件、重连);
  4. 扩展策略简述。

注意事项

互见


采编自 jeffallan/claude-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.