
Websocket Implementation
Give your coding agent copy-paste WebSocket and Socket.IO client patterns with reconnect, auth handshakes, and offline message queues for live SaaS features.
Overview
Websocket-implementation is an agent skill for the Build phase that guides solo builders through browser WebSocket client setup with reconnection, authentication, and queued messaging.
Install
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill websocket-implementationWhat is this skill?
- Reusable WebSocketClient class with configurable max reconnect attempts and backoff delay
- Socket.IO connect/disconnect/error and connect_error listeners with queued flush on reconnect
- Auth emit flow with success callback and isAuthenticated gate before sensitive events
- Listener map and on() registration pattern for extensible event routing
- Message queue processing when the socket comes back online
Adoption & trust: 526 installs on skills.sh; 251 GitHub stars; 2/3 security scanners passed (skills.sh audits).
What problem does it solve?
You need live updates in your app but keep shipping fragile one-off socket code that drops messages and never recovers from disconnects.
Who is it for?
Solo builders adding Socket.IO or similar realtime layers to a React or vanilla JS SaaS after the REST API exists.
Skip if: Teams that only need occasional polling, serverless-only architectures with no persistent connections, or projects already standardized on a managed realtime BaaS with its own SDK.
When should I use this skill?
Implementing browser WebSocket or Socket.IO-style clients with reconnection, authentication, and offline message handling.
What do I get? / Deliverables
You get a structured client pattern with reconnect limits, auth handshake, and queue flush so realtime features behave predictably under network churn.
- WebSocketClient class or equivalent module
- Documented event and auth flow for your feature
Recommended Skills
Journey fit
Real-time transport belongs on the Build shelf under integrations because it wires the product to persistent server connections after core APIs exist. Browser and server WebSocket clients are integration glue between your UI and realtime backends—not generic frontend styling.
How it compares
Use as an integration template skill rather than asking the agent to invent socket lifecycle code from scratch each sprint.
Common Questions / FAQ
Who is websocket-implementation for?
Indie and solo developers building SaaS or internal tools who want agent-guided WebSocket client structure instead of ad-hoc snippets from blog posts.
When should I use websocket-implementation?
During Build integrations when wiring chat, live feeds, or collaborative UIs; also when debugging reconnect and auth issues before Ship performance testing.
Is websocket-implementation safe to install?
Review the Security Audits panel on this Prism page and inspect the skill source in your repo before granting network or shell access to your agent.
SKILL.md
READMESKILL.md - Websocket Implementation
# Browser WebSocket Client ## Browser WebSocket Client ```javascript class WebSocketClient { constructor(url, options = {}) { this.url = url; this.socket = null; this.reconnectAttempts = 0; this.maxReconnectAttempts = options.maxReconnectAttempts || 5; this.reconnectDelay = options.reconnectDelay || 1000; this.listeners = new Map(); this.messageQueue = []; this.isAuthenticated = false; this.connect(); } connect() { this.socket = io(this.url, { reconnection: true, reconnectionDelay: this.reconnectDelay, reconnectionAttempts: this.maxReconnectAttempts, }); this.socket.on("connect", () => { console.log("Connected to server"); this.reconnectAttempts = 0; this.processMessageQueue(); }); this.socket.on("disconnect", () => { console.log("Disconnected from server"); }); this.socket.on("error", (error) => { console.error("Socket error:", error); this.emit("error", error); }); this.socket.on("connect_error", (error) => { console.error("Connection error:", error); }); } authenticate(userData) { this.socket.emit("auth", userData, (response) => { if (response.success) { this.isAuthenticated = true; this.emit("authenticated"); } }); } on(event, callback) { this.socket.on(event, callback); if (!this.listeners.has(event)) { this.listeners.set(event, []); } this.listeners.get(event).push(callback); } emit(event, data, callback) { if (!this.socket.connected) { this.messageQueue.push({ event, data, callback }); return; } this.socket.emit(event, data, callback); } processMessageQueue() { while (this.messageQueue.length > 0) { const { event, data, callback } = this.messageQueue.shift(); this.socket.emit(event, data, callback); } } joinRoom(roomId) { this.emit("room:join", roomId); } leaveRoom(roomId) { this.emit("room:leave", roomId); } sendMessage(roomId, text) { this.emit("chat:message", { roomId, text }); } setTypingIndicator(roomId, isTyping) { if (isTyping) { this.emit("typing:start", roomId); } else { this.emit("typing:stop", roomId); } } disconnect() { this.socket.disconnect(); } } // Usage const client = new WebSocketClient("http://localhost:3000"); client.on("chat:message", (message) => { console.log("Received message:", message); displayMessage(message); }); client.on("typing:indicator", (data) => { updateTypingIndicator(data); }); client.on("user:online", (user) => { updateUserStatus(user.userId, "online"); }); client.authenticate({ id: "user123", username: "john" }); client.joinRoom("room1"); client.sendMessage("room1", "Hello everyone!"); ``` # Message Types and Protocols ## Message Types and Protocols ```json // Authentication { "type": "auth", "userId": "user123", "token": "jwt_token_here" } // Chat Message { "type": "message", "roomId": "room123", "text": "Hello everyone!", "timestamp": "2025-01-15T10:30:00Z" } // Typing Indicator { "type": "typing", "roomId": "room123", "isTyping": true } // Presence { "type": "presence", "status": "online|away|offline" } // Notification { "type": "notification", "title": "New message", "body": "You have a new message", "data": {} } ``` # Node.js WebSocket Server (Socket.IO) ## Node.js WebSocket Server (Socket.IO) ```javascript const express = require("express"); const http = require("http"); const socketIo = require("socket.io"); const redis = require("redis"); const app = express(); const server = http.createServer(app); const io = socketIo(server, { cors: { origin: "*" }, transports: ["websocket", "polling"], reconnection: true, reconnectionDelay: 1000, reconnectionDelayMax: 5000, reconnectionAttempts: 5, }); // Redis adapter for horizontal scaling const redisClient = redis.createClient();