Chat App uses Socket.io on the same port as the REST API (defaultDocumentation 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.
4000). When the Express server starts, a Socket.io Server instance is mounted on the same underlying http.Server, so no extra port or proxy configuration is required. The server maintains an in-memory map of connected users (userSocketMap) that links each userId to its current socketId, enabling both real-time presence tracking and targeted message delivery.
Connection Setup
The client initiates a connection by passing the authenticated user’s ID as a query parameter. The server stores that mapping on theconnection event and removes it on disconnect.
The socket connects automatically as soon as
authUser is set in
AuthContext (login or page refresh with a valid cookie) and disconnects
when authUser is cleared (logout). This lifecycle is managed entirely inside
SocketContextProvider — you do not need to call connect() or close()
manually in your components.Events Overview
| Event | Direction | Trigger |
|---|---|---|
connection | Client → Server | User loads the app while authenticated |
disconnect | Client → Server | User closes the tab or logs out |
getOnlineUsers | Server → All Clients | Any user connects or disconnects |
newMessage | Server → One Client | A message is sent to this specific user |
getOnlineUsers
Direction: Server → All Clients (broadcast via io.emit)
Trigger: Fires on every connection and disconnect event so every connected client always has an up-to-date presence list.
Payload: string[] — an array of user IDs that are currently online.
SocketContextProvider stores the payload in onlineUsers state and exposes it through useSocketContext(). The sidebar uses this list to render a green indicator dot next to each contact:
userSocketMap is an in-memory object — it is not persisted to MongoDB.
Restarting the server resets all presence state, and every connected client
will receive a fresh getOnlineUsers broadcast once they reconnect.newMessage
Direction: Server → Target Client only (via io.to(socketId).emit)
Trigger: Fires from the sendMessage controller immediately after a message document is saved to MongoDB, but only when the receiver currently has an active socket connection:
MongoDB ObjectId of the message document.
MongoDB ObjectId of the user who sent the message.
MongoDB ObjectId of the user who received the message.
The plaintext content of the message.
ISO 8601 timestamp set by MongoDB when the document was created.
ISO 8601 timestamp updated by MongoDB on every document change.
useListenMessages: When the event arrives, the hook plays notification.mp3, sets a shouldShake: true flag on the message object (used by the message bubble animation), and appends the message to the active conversation state:
If the receiver is offline when the message is sent,
getReceiverSocketId
returns undefined and no socket event is emitted. The message is still
persisted in MongoDB and will appear in full when the receiver opens the
conversation — Chat App fetches the full message history via
GET /api/messages/:id on conversation select.