← ClaudeAtlas

websocket-implementationlisted

Implements real-time WebSocket communication with connection management, room-based messaging, and horizontal scaling. Use when building chat systems, live notifications, collaborative tools, or real-time dashboards.
secondsky/claude-skills · ★ 248 · Web & Frontend · score 90
Install: claude install-skill secondsky/claude-skills
# WebSocket Implementation Build scalable real-time communication systems with proper connection management. ## Server Implementation (Socket.IO) ```javascript const { Server } = require('socket.io'); const { createAdapter } = require('@socket.io/redis-adapter'); const { createClient } = require('redis'); const io = new Server(server, { cors: { origin: process.env.CLIENT_URL, credentials: true } }); // Redis adapter for horizontal scaling const pubClient = createClient({ url: process.env.REDIS_URL }); const subClient = pubClient.duplicate(); Promise.all([pubClient.connect(), subClient.connect()]).then(() => { io.adapter(createAdapter(pubClient, subClient)); }); // Connection management const users = new Map(); io.use((socket, next) => { const token = socket.handshake.auth.token; try { socket.user = verifyToken(token); next(); } catch (err) { next(new Error('Authentication failed')); } }); io.on('connection', (socket) => { users.set(socket.user.id, socket.id); console.log(`User ${socket.user.id} connected`); socket.on('join-room', (roomId) => { socket.join(roomId); socket.to(roomId).emit('user-joined', socket.user); }); socket.on('message', ({ roomId, content }) => { io.to(roomId).emit('message', { userId: socket.user.id, content, timestamp: Date.now() }); }); socket.on('disconnect', () => { users.delete(socket.user.id); }); }); // Utility methods for message distribution function broadca