websocket-architecture

Documents Socket.IO real-time architecture across NestJS gateway and React Redux client.

1|Updated Jun 9, 2026
One-click install
npx skills add https://github.com/nqaiservice-nikhil-kumar/Social-media-application --skill websocket-architecture-nqaiservice-nikhil-kumar
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: websocket-architecture
Source: https://github.com/nqaiservice-nikhil-kumar/Social-media-application/tree/main/.agents/skills/websocket-architecture
Command: npx skills add https://github.com/nqaiservice-nikhil-kumar/Social-media-application --skill websocket-architecture-nqaiservice-nikhil-kumar

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Real-time features in this social media app span a NestJS Socket.IO gateway, JWT-authenticated connections, presence tracking, and RTK Query cache updates on the React client, making it hard to add or debug real-time behavior without knowing the full event flow. ## Core Features & Use Cases - End-to-End Event Reference: Maps every Socket.IO event (newMessage, newNotification, userOnline, syncOnlineUsers, newPostInFeed) from server emitter to client consumer. - Server Architecture Guide: Explains the ChatGateway, JWT handshake authentication, room naming conventions, and the NotificationModule circular dependency resolved with forwardRef. - Client Patterns: Documents the socket singleton utility, Redux online-presence slice, and the RTK Query onCacheEntryAdded listener pattern with mandatory cleanup. - Use Case: When adding a new real-time feature like live typing indicators, consult this Skill to follow the established gateway, room naming, and cache-update patterns without introducing duplicate listeners. ## Quick Start Use the websocket-architecture skill to explain how to add a new real-time event from the NestJS gateway to the React feed.

Frequently Asked Questions about websocket-architecture

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

FAQPage Schema
How do I add a new real-time event with Socket.IO in NestJS and React?

Emit the event from the NestJS gateway using server.to(room).emit(), then register a listener on the React client inside an RTK Query onCacheEntryAdded hook. Always call socket.off(event, listener) after cacheEntryRemoved to prevent duplicate listeners.

How does Socket.IO JWT authentication work in NestJS?

The client passes the token via handshake auth as Bearer <token>, and the server reads client.handshake.auth.token in the gateway. A helper verifies it with JwtService and the app secret, extracting the userId or setting it to null if invalid.

Why do I get duplicate Socket.IO messages in React?

Duplicate messages usually come from missing listener cleanup. Always pair socket.on with socket.off(event, listener) after await cacheEntryRemoved in RTK Query or in a useEffect cleanup, and use the shared socket singleton instead of creating new io() connections.

How do I handle circular dependencies between NestJS modules?

Use forwardRef(() => Module) on both module imports and forwardRef(() => Service) on both injected providers. In this app, ChatModule and NotificationModule reference each other and require forwardRef on both sides.

Can I use React Context to manage a Socket.IO connection?

This codebase intentionally avoids React Context for sockets; the ScoketContext file is empty. Use the module-level singleton in utils/socket.ts via initializeSocket(), which returns the same socket instance across the app.