websocket-socketio-patterns

Implements Socket.IO real-time patterns for authentication, rooms, presence, and scaling in Node.js and React.

Updated Aug 11, 2026
One-click install
npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill websocket-socketio-patterns-duccuong159
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: websocket-socketio-patterns
Source: https://github.com/DucCuong159/Realtime-chatapp/tree/main/.agent/skills/websocket-socketio-patterns
Command: npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill websocket-socketio-patterns-duccuong159

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires socket.io, socket.io-client, jsonwebtoken, cookie, @socket.io/redis-adapter.

What problem does it solve? Building real-time features like chat, notifications, and live presence with Socket.IO involves tricky edge cases: unauthenticated connections, duplicate messages across tabs, inaccurate online status, and reconnect storms. This Skill provides proven patterns and type-safe code templates to implement these features correctly the first time. ## Core Features & Use Cases - Type-Safe Server Setup: Strictly typed Server-to-Client and Client-to-Server event interfaces, plus JWT/cookie authentication middleware at the handshake stage. - Rooms & Reliable Delivery: User-specific room patterns (user:${userId}), conversation channels, and acknowledgement callbacks for confirmed message persistence. - Presence & Typing Indicators: Multi-tab aware online/offline tracking using per-user socket counts, plus debounced typing broadcasts. - React Client & Scaling: Singleton socket hook pattern to prevent reconnect loops, heartbeat configuration, CORS hardening, and Redis adapter clustering for multi-node deployments. - Use Case: When adding a live chat feature to an Express and React app, use this Skill to scaffold the authenticated Socket.IO server, room routing, presence tracking, and the React connection hook. ## Quick Start Ask the AI to implement a Socket.IO server with JWT authentication, conversation rooms, and online presence tracking for an Express and React chat app.

Frequently Asked Questions about websocket-socketio-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I authenticate Socket.IO connections with JWT?

Validate the JWT in a Socket.IO middleware during the handshake, before the connection is established. Parse the token from cookies or handshake.auth, verify it with jsonwebtoken, attach the user to socket.data, and call next() with an error to reject unauthenticated clients.

How to track online presence with Socket.IO across multiple tabs?

Track a per-user socket count in a Map or Redis Set rather than relying on a single disconnect event. Broadcast online status only when the first socket connects, and offline status only when the count reaches zero, so users with multiple tabs stay online.

How do I send a Socket.IO message to a specific user?

Join each user to a personal room named user:${userId} when they connect. Then emit to that room with io.to(`user:${userId}`) to reach all of the user's active devices without tracking individual socket IDs.

Why does my React app create duplicate Socket.IO connections?

Initializing io() directly inside a component body creates a new connection on every re-render. Store the socket in a module-level singleton or ref, create it lazily with autoConnect: false, and manage connect/disconnect inside useEffect.

Can Socket.IO scale across multiple Node.js server instances?

Yes, attach @socket.io/redis-adapter or @socket.io/redis-streams-adapter so events broadcast on one node reach clients connected to other nodes. This is required whenever running more than one backend instance or cluster.

How do I confirm a Socket.IO message was saved to the database?

Use acknowledgement callbacks: the client passes a callback function with the emit, and the server invokes it after persisting the message. Return { status: "ok", data } on success or { status: "error", message } on failure so the client can reconcile optimistic UI.