Skip to content
Back to skills

Websocket Realtime Handler

ASecurity

Implement WebSocket connection lifecycles, ping/pong heartbeats, reconnect logic, and event broadcasting.

  • 2 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 27, 2026
developmentjavascriptjavanodeapi

Works with

  • cli
  • api

Security analysis

A100/100

Scanned September 27, 2026

npx -y skills add RMAzrim/opencode-artes --skill websocket-realtime-handler --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Websocket Realtime Handler?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Websocket Realtime Handler
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/rmazrim-websocket-realtime-handler/badge)](https://www.skillsdirectory.com/skills/rmazrim-websocket-realtime-handler)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: WebSocket Realtime Handler
description: Implement WebSocket connection lifecycles, ping/pong heartbeats, reconnect logic, and event broadcasting.
metadata:
  source: skills/websocket-realtime-handler/websocket-realtime-handler.md
---

# WebSocket Realtime Handler

## Prerequisites & Dependencies
- Node.js 18+ with `ws` or `socket.io` library
- Understanding of browser WebSocket API and same-origin policy
- Optional: Redis for distributed pub/sub across multiple server instances

## Execution Steps
1. Set up a WebSocket server (`ws` lightweight, `socket.io` feature-rich) on an HTTPS endpoint
2. Implement the connection lifecycle: `connection`, `open`, `message`, `close` events
3. Add a periodic ping/pong heartbeat (e.g., every 25s, timeout after 60s) to detect dead connections
4. Implement graceful reconnect logic on the client: exponential backoff, rejoin rooms/channels after reconnection
5. For broadcasting: use `socket.emit('event', data)` for single room, or Redis pub/sub (`socket.io-redis`) for multi-instance setups
6. Secure the WS handshake: validate JWT cookies/origins, reject unauthorized connections
7. Log connection metrics (connections per minute, disconnect reasons) for observability

```javascript
// ws server with heartbeat and room broadcasting
const WebSocket = require('ws');
const server = new WebSocket.Server({ noServer: true, path: '/ws' });

const HEARTBEAT_INTERVAL = 25000;
const HEARTBEAT_TIMEOUT = 60000;

server.on('connection', (ws) => {
  ws.isAlive = true;
  ws.on('pong', () => { ws.isAlive = true; });
  ws.on('close', () => { console.log('client disconnected'); });

  const heartbeat = setInterval(() => {
    ws.ping(null, (err) => {
      if (err) { clearInterval(heartbeat); ws.terminate(); }
    });
  }, HEARTBEAT_INTERVAL);

  ws.on('message', (msg) => {
    // broadcast to room 'auction-123'
    wss.clients.forEach(client => {
      if (client.room === 'auction-123') client.send(msg);
    });
  });
});

setInterval(() => {
  server.clients.forEach(ws => {
    if (ws.isAlive === false) return ws.terminate();
    ws.isAlive = false;
    ws.ping();
  });
}, 30000);
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.