home / skills / a5c-ai / babysitter / websocket

This skill helps you implement robust WebSocket real-time features, including server setup, connection lifecycle, scaling, and authentication.

npx playbooks add skill a5c-ai/babysitter --skill websocket

Review the files below or copy the command above to add this skill to your agents.

Files (2)
SKILL.md
1.5 KB
---
name: websocket
description: WebSocket implementation, connection management, scaling patterns, and real-time features.
allowed-tools: Read, Write, Edit, Bash, Glob, Grep
---

# WebSocket Skill

Expert assistance for implementing WebSocket real-time communication.

## Capabilities

- Implement WebSocket servers
- Handle connection lifecycle
- Build pub/sub patterns
- Scale with Redis adapter
- Implement reconnection logic
- Handle authentication

## Usage

Invoke this skill when you need to:
- Add real-time features
- Build chat applications
- Implement live updates
- Handle bidirectional communication

## Server Implementation

```typescript
import { WebSocketServer, WebSocket } from 'ws';
import { createServer } from 'http';

const server = createServer();
const wss = new WebSocketServer({ server });

const clients = new Map<string, WebSocket>();

wss.on('connection', (ws, req) => {
  const userId = authenticateConnection(req);
  clients.set(userId, ws);

  ws.on('message', (data) => {
    const message = JSON.parse(data.toString());
    handleMessage(userId, message);
  });

  ws.on('close', () => {
    clients.delete(userId);
  });
});

function broadcast(message: object) {
  const data = JSON.stringify(message);
  clients.forEach((client) => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(data);
    }
  });
}
```

## Best Practices

- Implement heartbeat/ping-pong
- Handle reconnection gracefully
- Use message queues for scaling
- Authenticate connections

## Target Processes

- real-time-features
- chat-application
- live-updates

Overview

This skill provides practical guidance and code patterns for building WebSocket-based real-time systems in JavaScript. It focuses on connection lifecycle management, pub/sub patterns, scaling techniques, and secure reconnection flows. Use it to add deterministic, resumable real-time behavior to agent orchestration and interactive apps.

How this skill works

The skill inspects common WebSocket server and client workflows: accepting connections, authenticating users, routing messages, and cleaning up on disconnect. It describes pub/sub and broadcast patterns, heartbeat/ping-pong health checks, and using Redis or message queues as adapters to scale across processes. It also outlines reconnection strategies and where to place authentication and rate limiting.

When to use it

  • Adding chat, presence, or live-collaboration features to an app
  • Delivering real-time updates from backend processes to clients
  • Building bidirectional agent orchestration or control channels
  • Scaling a WebSocket service across multiple nodes or containers
  • Implementing resilient reconnection and session resumption

Best practices

  • Authenticate connections early and attach user/session metadata to sockets
  • Implement heartbeat (ping/pong) to detect stale clients and free resources
  • Use pub/sub or Redis adapter to broadcast messages across multiple server instances
  • Persist or sequence critical events for resumable clients and deterministic replay
  • Throttle and validate incoming messages to protect against spikes and abuse

Example use cases

  • Chat application: route messages to specific user sockets and broadcast to rooms
  • Live dashboards: push metrics and state changes from workers to subscribed clients
  • Agent orchestration: control and receive events from distributed agent workers in real time
  • Collaborative editing: propagate edits with ordering and conflict mitigation
  • Scaling pattern: attach a Redis adapter to sync client lists and broadcasts between nodes

FAQ

How should I handle connection authentication?

Authenticate during the initial WebSocket handshake using headers or a short-lived token, then store user ID/session info on the socket for routing and authorization checks.

When do I need a Redis adapter?

Use a Redis or message-queue adapter when your service runs multiple server instances and you need to broadcast messages or sync presence across processes.