Loading skill
Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Covers WebSocket connections, room grouping, reconnection, message buffering, and binary protocol design in TypeScript with the ws library.
8% 21%+13 ptsof test cases passed
Passed 5 of 24 test cases with the skill, and 2 of 24 without it.
3 of 3 safety checks passed
Not used yet
Production WebSocket patterns for real-time applications.
import { WebSocketServer, WebSocket } from 'ws'
interface Client {
id: string
ws: WebSocket
rooms: Set<string>
lastPing: number
metadata: Record<string, unknown>
}
class ConnectionManager {
private clients = new Map<string, Client>()
private heartbeatInterval: NodeJS.Timeout
constructor(private wss: WebSocketServer) {
this.heartbeatInterval = setInterval(() => this.checkHeartbeats(), 30_000)
wss.on('connection', (ws, req) => {
const clientId = crypto.randomUUID()
const client: Client = {
id: clientId,
ws,
rooms: new Set(),
lastPing: Date.now(),
metadata: { ip: req.socket.remoteAddress }
}
this.clients.set(clientId, client)
ws.on('pong', () => { client.lastPing = Date.now() })
ws.on('close', () => this.removeClient(clientId))
ws.on('error', () => this.removeClient(clientId))
this.send(client, { type: 'connected', clientId })
Next to this one in the catalogue, plus more from the same author.