Chat App persists every exchange in MongoDB and delivers new messages instantly over a persistent Socket.io connection. Conversations are created lazily — the first message between two users automatically provisions the shared conversation document, so there is no separate “start chat” step.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.
Conversation Model
AConversation document ties two users together and acts as a container for their message history.
| Field | Type | Description |
|---|---|---|
participants | [ObjectId] | Array of exactly two User _id references |
messages | [ObjectId] | Ordered array of Message _id references |
Message document stores the payload for a single chat bubble:
| Field | Type | Description |
|---|---|---|
senderId | ObjectId | User who sent the message |
receiverId | ObjectId | User who should receive it |
message | string | Plain-text message body |
createdAt / updatedAt | Date | Mongoose timestamps |
The server queries conversations with
{ participants: { $all: [senderId, receiverId] } }, so the participant order in the array does not matter.Sending a Message
Endpoint:POST /api/messages/send/:receiverIdAuth: Required (
protectRoute middleware)Body:
{ "message": "Hello!" }
Locate or create the conversation
The backend searches for an existing
Conversation whose participants array contains both senderId (from req.user._id) and :receiverId. If none exists, it creates one on the spot.Create the Message document
A new
Message is instantiated with senderId, receiverId, and the message string from the request body.Link message to conversation
The new message’s
_id is pushed into conversation.messages so the history stays ordered.Persist both documents in parallel
Emit to receiver's socket (if online)
userSocketMap and the message is emitted directly to that socket — no polling required.201 Created
Fetching Message History
Endpoint:GET /api/messages/:idAuth: Required The backend looks up the
Conversation for the current user and the target user (:id), then calls .populate("messages") to replace the ObjectId references with full Message documents. If no conversation exists yet, an empty array [] is returned — not a 404 error.
The useGetMessages hook fetches history automatically whenever selectedConversation changes:
Incoming Message Notifications
useListenMessages registers a Socket.io listener for the "newMessage" event while a conversation is open:
- Notification sound —
notification.mp3is played so the user is alerted even if the window is in the background. - Shake animation flag —
shouldShake = trueis set on the message object. UI components can check this property to apply a CSS shake animation to the new bubble.
Sidebar Search
The sidebar search is a client-side filter — no extra API call is made.SearchInput holds a text state and, on form submission, filters the already-loaded conversations list by fullName:
The search input requires at least 3 characters before attempting a match. Shorter queries are rejected with a toast notification to avoid false positives on very short names.
Frontend Messaging Hooks
| Hook | What it does | Returns |
|---|---|---|
useSendMessage | POSTs a message to /api/messages/send/:id and appends the result to the Zustand messages array | { sendMessage, loading } |
useGetMessages | Fetches full message history for selectedConversation on mount and when the conversation changes | { messages, loading } |
useListenMessages | Subscribes to the "newMessage" socket event; plays a sound and sets shouldShake | void (no return value) |
Using useSendMessage in a component
Zustand Conversation Store
Global messaging state is managed by theuseConversation Zustand store, making it accessible from any component without prop drilling:
| State key | Type | Description |
|---|---|---|
selectedConversation | object | null | The contact/conversation currently open in the chat panel |
messages | array | The message history for selectedConversation |