websockets-realtime
Real-time communication with WebSockets, Server-Sent Events, and related technologies. Use when building chat, live updates, collaborative features, or any real-time functionality.
What this skill does
# WebSockets & Real-Time
Comprehensive guide for building real-time applications.
## Real-Time Technologies
### Comparison
| Technology | Direction | Use Case |
| ---------------------- | ----------------------- | --------------------------- |
| **WebSocket** | Bidirectional | Chat, gaming, collaboration |
| **Server-Sent Events** | Server → Client | Live feeds, notifications |
| **Long Polling** | Simulated bidirectional | Fallback, simple updates |
| **WebRTC** | Peer-to-peer | Video calls, file sharing |
### When to Use What
```
WEBSOCKETS:
✓ Chat applications
✓ Real-time collaboration
✓ Gaming
✓ Financial trading
✓ IoT dashboards
✓ Any bidirectional communication
SERVER-SENT EVENTS (SSE):
✓ Live feeds (news, sports)
✓ Notifications
✓ Progress updates
✓ Server-initiated updates only
LONG POLLING:
✓ Fallback when WebSocket unavailable
✓ Simple, infrequent updates
✓ Behind strict firewalls
WEBRTC:
✓ Video/audio calls
✓ Screen sharing
✓ Peer-to-peer file transfer
```
---
## WebSocket Fundamentals
### How WebSockets Work
```
HTTP Upgrade Handshake:
┌──────┐ ┌──────┐
│Client│ GET /ws HTTP/1.1 │Server│
│ │ Upgrade: websocket │ │
│ │ ──────────────────> │ │
│ │ │ │
│ │ HTTP/1.1 101 │ │
│ │ Switching Protocols │ │
│ │ <────────────────── │ │
└──────┘ └──────┘
After handshake:
┌──────┐ ┌──────┐
│Client│ <═══════════════════>│Server│
│ │ Full-duplex TCP │ │
│ │ Binary or text │ │
└──────┘ └──────┘
```
### Client Implementation
```typescript
// Basic WebSocket client
const ws = new WebSocket("wss://api.example.com/ws");
ws.onopen = () => {
console.log("Connected");
ws.send(JSON.stringify({ type: "subscribe", channel: "updates" }));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};
ws.onerror = (error) => {
console.error("WebSocket error:", error);
};
ws.onclose = (event) => {
console.log("Disconnected:", event.code, event.reason);
};
// Send message
ws.send(JSON.stringify({ type: "message", content: "Hello!" }));
// Close connection
ws.close(1000, "Normal closure");
```
### Reconnection Logic
```typescript
class ReconnectingWebSocket {
private ws: WebSocket | null = null;
private reconnectAttempts = 0;
private maxReconnectAttempts = 10;
private reconnectDelay = 1000;
constructor(private url: string) {
this.connect();
}
private connect() {
this.ws = new WebSocket(this.url);
this.ws.onopen = () => {
console.log("Connected");
this.reconnectAttempts = 0;
};
this.ws.onclose = (event) => {
if (event.code !== 1000) {
this.reconnect();
}
};
this.ws.onerror = () => {
this.ws?.close();
};
}
private reconnect() {
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
console.error("Max reconnection attempts reached");
return;
}
this.reconnectAttempts++;
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
console.log(
`Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})`,
);
setTimeout(() => this.connect(), delay);
}
send(data: unknown) {
if (this.ws?.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(data));
}
}
}
```
---
## Server Implementation (Node.js)
### ws Library
```typescript
import { WebSocketServer, WebSocket } from "ws";
import { createServer } from "http";
const server = createServer();
const wss = new WebSocketServer({ server });
// Track connected clients
const clients = new Set<WebSocket>();
wss.on("connection", (ws, request) => {
console.log("Client connected");
clients.add(ws);
// Send welcome message
ws.send(JSON.stringify({ type: "connected", clientCount: clients.size }));
ws.on("message", (data) => {
try {
const message = JSON.parse(data.toString());
handleMessage(ws, message);
} catch (error) {
ws.send(JSON.stringify({ type: "error", message: "Invalid JSON" }));
}
});
ws.on("close", () => {
clients.delete(ws);
console.log("Client disconnected");
});
ws.on("error", (error) => {
console.error("WebSocket error:", error);
});
// Heartbeat to detect stale connections
ws.isAlive = true;
ws.on("pong", () => {
ws.isAlive = true;
});
});
// Heartbeat interval
const heartbeatInterval = setInterval(() => {
wss.clients.forEach((ws) => {
if (!ws.isAlive) {
return ws.terminate();
}
ws.isAlive = false;
ws.ping();
});
}, 30000);
wss.on("close", () => {
clearInterval(heartbeatInterval);
});
function handleMessage(ws: WebSocket, message: any) {
switch (message.type) {
case "broadcast":
broadcast(message.content);
break;
case "private":
// Handle private messages
break;
default:
ws.send(
JSON.stringify({ type: "error", message: "Unknown message type" }),
);
}
}
function broadcast(content: any) {
const message = JSON.stringify({ type: "broadcast", content });
clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
}
server.listen(3000);
```
### Socket.IO
```typescript
import { Server } from "socket.io";
import { createServer } from "http";
const httpServer = createServer();
const io = new Server(httpServer, {
cors: {
origin: "https://example.com",
methods: ["GET", "POST"],
},
});
// Namespace for chat
const chat = io.of("/chat");
chat.on("connection", (socket) => {
console.log("User connected:", socket.id);
// Join room
socket.on("join", (room: string) => {
socket.join(room);
socket.to(room).emit("user_joined", { userId: socket.id });
});
// Handle message
socket.on("message", (data: { room: string; content: string }) => {
chat.to(data.room).emit("message", {
from: socket.id,
content: data.content,
timestamp: Date.now(),
});
});
// Leave room
socket.on("leave", (room: string) => {
socket.leave(room);
socket.to(room).emit("user_left", { userId: socket.id });
});
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
});
});
// Authentication middleware
io.use((socket, next) => {
const token = socket.handshake.auth.token;
if (validateToken(token)) {
socket.data.user = decodeToken(token);
next();
} else {
next(new Error("Authentication error"));
}
});
httpServer.listen(3000);
```
---
## Server-Sent Events (SSE)
### Server Implementation
```typescript
import express from "express";
const app = express();
app.get("/events", (req, res) => {
// Set SSE headers
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
// Send initial event
res.write("event: connected\n");
res.write('data: {"status": "connected"}\n\n');
// Send periodic updates
const interval = setInterval(() => {
const data = JSON.stringify({
timestamp: Date.now(),
value: Math.random(),
});
res.write(`data: ${data}\n\n`);
}, 1000);
// Cleanup on disconnect
req.on("close", () => {
clearInterval(interval);
res.end();
});
});
app.listen(3000);
```
### Client Implementation
```typescript
const eventSource = new EventSource("/events");
eventSource.onopen = () => {
console.log("SSE connection opened");
};
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log("Received:", data);
};
eventSource.addEventListener("connected", (event) => {
console.log("Connected event:", event.data);
});
eventSource.onerror = (error) => {
console.error("Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.