TalkBox uses Socket.IO v4 to power real-time messaging and presence updates. Every WebSocket connection is authenticated using a JSON Web Token (JWT) — the server rejects any connection that does not supply a valid token in the handshake. Once authenticated, clients can emit events to send messages and receive live updates pushed directly from the server.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.
Connection Setup
The client-side socket instance is created once and exported as a singleton. It is initialised withautoConnect: false so that the connection is only established after a JWT has been obtained — preventing unauthenticated connection attempts on page load.
Connection URL: resolved from the VITE_API_URL environment variable.
auth object before calling socket.connect(). Socket.IO forwards this object as socket.handshake.auth on the server.
Authentication
The server registers a Socket.IO middleware that runs before any event handler. It reads the token fromsocket.handshake.auth.token, verifies it with jsonwebtoken, and fetches the matching User document from MongoDB. On success, the user document is attached to socket.user so all downstream event handlers have access to it. Any connection that supplies a missing, expired, or otherwise invalid token is rejected immediately with Error("Unauthorized.").
Client → Server Events
These are the events your client application emits to the TalkBox server.join
Registers the authenticated user as online. Emit this event immediately after connecting — untiljoin is called, the server does not map the user’s ID to their socket, so they will not appear in the online-users list and cannot receive real-time messages.
Payload: none
Server behavior:
- Adds a
userId → socketIdentry to the in-memoryonlineUsersMap. - Broadcasts the updated online-user list to all connected clients via the
onlineUsersevent.
sendMessage
Sends a message to another user in real time. The server persists the message to MongoDB before delivering it, so the message is durable even if the recipient is currently offline. Payload:MongoDB
ObjectId of the intended recipient. Must correspond to an existing
user in the database.The plain-text content of the message.
- Creates a
Messagedocument in MongoDB withsender,receiver, andcontentfields. - Populates the
senderandreceiverfields with theirusernamevalues. - Emits
newMessageback to the sender’s socket. - Emits
newMessageto the receiver’s socket (if they are currently online).
Server → Client Events
These are the events the TalkBox server pushes to your client. Attach listeners withsocket.on(eventName, handler) and remove them with socket.off(eventName) when the component unmounts to avoid memory leaks.
newMessage
Emitted to both the sender and the receiver after a message has been successfully persisted in MongoDB. Because both parties receive the same populated document, no additional API fetch is required to display a new message in the UI. Payload:onlineUsers
Broadcast to all connected clients whenever the presence state changes — that is, whenever any user emitsjoin or disconnects. Use this event to keep an online-indicator UI in sync without polling.
Payload: an array of MongoDB ObjectId strings representing every user who is currently connected.
Full Client Example
The snippet below shows a complete, self-contained usage of the TalkBox WebSocket API: importing the socket singleton, authenticating, registering presence, sending messages, listening for incoming messages and presence changes, and cleaning up on teardown.Socket.IO automatically falls back to HTTP long-polling when a WebSocket
connection cannot be established (for example, behind certain proxies or
firewalls). Real-time functionality is preserved in fallback mode, though with
slightly higher latency. No code changes are needed on the client to enable
this behaviour.