jpskill.com
🛠️ 開発・MCP コミュニティ

pitfalls-websocket

WebSocket server and client patterns with heartbeat and reconnection. Use when implementing real-time features, debugging connection issues, or reviewing WebSocket code. Triggers on: WebSocket, wss, heartbeat, reconnect, real-time.

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o pitfalls-websocket.zip https://jpskill.com/download/17450.zip && unzip -o pitfalls-websocket.zip && rm pitfalls-websocket.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/17450.zip -OutFile "$d\pitfalls-websocket.zip"; Expand-Archive "$d\pitfalls-websocket.zip" -DestinationPath $d -Force; ri "$d\pitfalls-websocket.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して pitfalls-websocket.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → pitfalls-websocket フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

WebSocket の落とし穴

WebSocket の実装におけるよくある落とし穴と正しいパターンについて説明します。

どのような時に使うか

  • WebSocket サーバーを実装する時
  • 再接続機能を持つ WebSocket クライアントを構築する時
  • 接続断をデバッグする時
  • ハートビート/ping-pong を追加する時
  • WebSocket のコードをレビューする時

ワークフロー

ステップ 1: サーバー設定の確認

WebSocket サーバーが HTTP ポートを共有しているか確認します。

ステップ 2: ハートビートの確認

ping/pong ハートビートが実装されていることを確認します。

ステップ 3: クライアント再接続の確認

指数バックオフ再接続ロジックを確認します。


サーバーパターン

const wss = new WebSocketServer({ server: httpServer }); // Same port

wss.on('connection', (ws) => {
  // Heartbeat
  ws.isAlive = true;
  ws.on('pong', () => { ws.isAlive = true; });

  ws.on('message', (data) => {
    try {
      const msg = JSON.parse(data.toString());
      // Validate message type
    } catch {
      ws.send(JSON.stringify({ error: 'Invalid message' }));
    }
  });
});

// Heartbeat interval
setInterval(() => {
  wss.clients.forEach((ws) => {
    if (!ws.isAlive) return ws.terminate();
    ws.isAlive = false;
    ws.ping();
  });
}, 30000);

クライアント再接続

// Client - reconnection logic with exponential backoff
let attempt = 0;

function connect() {
  const ws = new WebSocket(url);

  ws.onopen = () => {
    attempt = 0; // Reset on successful connection
  };

  ws.onclose = () => {
    const delay = Math.min(1000 * Math.pow(2, attempt), 30000);
    attempt++;
    setTimeout(connect, delay);
  };

  ws.onerror = (error) => {
    console.error('WebSocket error:', error);
  };
}

メッセージ処理

// ✅ Always validate and type messages
interface WsMessage {
  type: 'subscribe' | 'unsubscribe' | 'ping';
  channel?: string;
}

function handleMessage(ws: WebSocket, data: string) {
  try {
    const msg: WsMessage = JSON.parse(data);

    switch (msg.type) {
      case 'subscribe':
        subscribeToChannel(ws, msg.channel);
        break;
      case 'ping':
        ws.send(JSON.stringify({ type: 'pong' }));
        break;
      default:
        ws.send(JSON.stringify({ error: 'Unknown message type' }));
    }
  } catch {
    ws.send(JSON.stringify({ error: 'Invalid JSON' }));
  }
}

簡単なチェックリスト

  • [ ] WebSocket サーバーは HTTP と同じポートを使用している
  • [ ] 30 秒ごとにハートビート ping/pong を行っている
  • [ ] クライアントは指数バックオフで再接続を行っている
  • [ ] メッセージは処理前に検証されている
  • [ ] 接続エラーはログに記録されている
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

WebSocket Pitfalls

Common pitfalls and correct patterns for WebSocket implementations.

When to Use

  • Implementing WebSocket server
  • Building WebSocket client with reconnection
  • Debugging connection drops
  • Adding heartbeat/ping-pong
  • Reviewing WebSocket code

Workflow

Step 1: Verify Server Setup

Check WebSocket server shares HTTP port.

Step 2: Check Heartbeat

Ensure ping/pong heartbeat is implemented.

Step 3: Verify Client Reconnection

Confirm exponential backoff reconnection logic.


Server Pattern

const wss = new WebSocketServer({ server: httpServer }); // Same port

wss.on('connection', (ws) => {
  // Heartbeat
  ws.isAlive = true;
  ws.on('pong', () => { ws.isAlive = true; });

  ws.on('message', (data) => {
    try {
      const msg = JSON.parse(data.toString());
      // Validate message type
    } catch {
      ws.send(JSON.stringify({ error: 'Invalid message' }));
    }
  });
});

// Heartbeat interval
setInterval(() => {
  wss.clients.forEach((ws) => {
    if (!ws.isAlive) return ws.terminate();
    ws.isAlive = false;
    ws.ping();
  });
}, 30000);

Client Reconnection

// Client - reconnection logic with exponential backoff
let attempt = 0;

function connect() {
  const ws = new WebSocket(url);

  ws.onopen = () => {
    attempt = 0; // Reset on successful connection
  };

  ws.onclose = () => {
    const delay = Math.min(1000 * Math.pow(2, attempt), 30000);
    attempt++;
    setTimeout(connect, delay);
  };

  ws.onerror = (error) => {
    console.error('WebSocket error:', error);
  };
}

Message Handling

// ✅ Always validate and type messages
interface WsMessage {
  type: 'subscribe' | 'unsubscribe' | 'ping';
  channel?: string;
}

function handleMessage(ws: WebSocket, data: string) {
  try {
    const msg: WsMessage = JSON.parse(data);

    switch (msg.type) {
      case 'subscribe':
        subscribeToChannel(ws, msg.channel);
        break;
      case 'ping':
        ws.send(JSON.stringify({ type: 'pong' }));
        break;
      default:
        ws.send(JSON.stringify({ error: 'Unknown message type' }));
    }
  } catch {
    ws.send(JSON.stringify({ error: 'Invalid JSON' }));
  }
}

Quick Checklist

  • [ ] WebSocket server uses same port as HTTP
  • [ ] Heartbeat ping/pong every 30 seconds
  • [ ] Client has reconnection with exponential backoff
  • [ ] Messages validated before processing
  • [ ] Connection errors logged