Chat App layers a Socket.io server on top of the same Node.js HTTP server that handles Express REST routes. This means a single process manages both traditional HTTP requests and long-lived WebSocket connections, eliminating the need for a separate real-time service. When a message is sent, the backend emits it directly to the recipient’s socket — the recipient’s browser receives it in milliseconds without polling.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/khushboodaryani/Chat-App/llms.txt
Use this file to discover all available pages before exploring further.
Socket.io Server Setup
The Socket.io server is created by wrapping the Expressapp in a native http.Server and passing that server to the Server constructor:
app (Express) and server (HTTP + Socket.io) are exported from this module so that route files and the entry point can import the same instances.
The CORS configuration allows connections from
http://localhost:3000 (the Vite dev server). In production the origin should be updated to match the deployed frontend URL.Connection Lifecycle
Client connects and registers userId
When a user logs in, the frontend opens a socket connection and passes the authenticated user’s On the server, the
_id as a query parameter:userId is read from socket.handshake.query.userId and stored in userSocketMap:Server broadcasts updated online user list
Immediately after storing the mapping, the server emits Every client’s
getOnlineUsers to every connected client with the current set of online user IDs:SocketContext updates its local onlineUsers array, so presence indicators across all open browser tabs refresh simultaneously.Message is sent to a specific socket
When the message controller needs to deliver a message in real time, it calls If the receiver is offline their entry will not be in
getReceiverSocketId to look up the target socket and uses io.to().emit() for a targeted emit:userSocketMap, and the condition short-circuits — the message is still persisted in MongoDB and will appear when the receiver next loads the conversation.Targeted Message Delivery
TheuserSocketMap object is the core of targeted delivery. It is a simple in-memory dictionary:
userId to retrieve a socketId, the server can call io.to(socketId).emit(event, data) which routes the event to exactly one connected client rather than broadcasting to all.
Frontend: SocketContext
The frontend manages the socket connection through a React context provider so that any component can accesssocket and onlineUsers via the useSocketContext() hook:
- The connection is opened as soon as
authUseris set (login/signup) and closed whenauthUserbecomesnull(logout). - The effect cleanup function (
return () => socket.close()) prevents duplicate connections ifauthUserchanges identity without an intermediate logout. onlineUsersis an array of user ID strings updated in real time by thegetOnlineUsersevent.
Event Reference
| Event name | Direction | Payload | Description |
|---|---|---|---|
getOnlineUsers | Server → All clients | string[] of user IDs | Broadcast whenever any user connects or disconnects |
newMessage | Server → specific client | Message document | Delivered to the receiver’s socket when a message is sent |