TalkBox delivers messages in real time using Socket.IO. When a user sends a message, the client emits a socket event; the server persists the document to MongoDB, populates the sender and receiver usernames, and immediately pushes the fully-formed message object back to both participants. Conversation history is loaded separately via a standard REST endpoint the first time a conversation is opened.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abdelhafid37/talkbox/llms.txt
Use this file to discover all available pages before exploring further.
How it works
Socket.IO
Handles live, bidirectional message delivery. Every new message is emitted to both the sender and receiver via their individual socket connections.
REST API
Used for loading existing conversation history when a chat is opened. Returns messages sorted chronologically from MongoDB.
Connection setup
The client creates a single shared socket instance withautoConnect: false, which prevents it from connecting immediately when the module is first imported:
AuthProvider once both a valid token and a loaded user object are available. Immediately after the connect event fires, the client emits a "join" event to register itself on the server:
Sending a message
When the user submits the message form inChatPage, the client emits a "sendMessage" event with the recipient’s ID and the message text:
"sendMessage" handler creates a Message document in MongoDB, populates the sender.username and receiver.username fields, then emits the populated message to both the sender’s and receiver’s sockets:
sender identity is taken from socket.user — the authenticated user object attached during the Socket.IO middleware handshake — so clients cannot spoof who sent a message.
Receiving messages
ChatPage listens for the "newMessage" event for the lifetime of a selected conversation. Before appending, it verifies that the incoming message belongs to the currently open conversation so messages from other threads are not mixed in:
selectedUser or user changes, ensuring no stale closures accumulate.
Messages are persisted to MongoDB regardless of whether the recipient is currently connected. Socket.IO delivery only works when the recipient has an active socket connection. If they are offline at the time the message is sent, they will see it the next time they open that conversation via the REST history endpoint.
Loading conversation history
When the user selects a contact in the sidebar,ChatPage calls the REST endpoint to fetch the full message history for that pair of users. The endpoint returns messages sorted ascending by createdAt:
GET /api/messages/:userId
The messageService helper attaches the JWT token from localStorage:
getConversationController queries MongoDB for all messages where the two users are either the sender or receiver, sorts them by createdAt ascending, and populates the username fields:
Message data shape
Each message returned by both the socket event and the REST endpoint has the following structure after population:createdAt timestamp is an ISO 8601 string and is formatted for display using date-fns in the MessageList component.