Overview
The MeetMates Socket.IO server handles real-time communication between users. This page covers the connection lifecycle, authentication, and disconnect handling.Connection Setup
Establishing a Connection
Direction: Client → Server Clients connect to the Socket.IO server using the standard Socket.IO client library. Authentication is optional but recommended for full feature access.http://localhost:5173(development)https://www.meetmates.space(production)
Authentication
Socket Authentication Middleware
Authentication is handled via JWT tokens passed during the Socket.IO handshake. Token Location:socket.handshake.auth.token(recommended)socket.handshake.headers.authorization(alternative)
- Bearer token:
Bearer <jwt-token> - Direct token:
<jwt-token>
io.use(socketAuthMiddleware), all connections require valid JWT tokens.
Connection Events
connection
Direction: Server-side event
Emitted when: A client successfully connects to the Socket.IO server
Server-side handler:
- Increments
onlineUserscounter - Stores authenticated user info if JWT token provided
- Broadcasts updated online count to all clients via
onlineUsersevent - Logs connection:
"New user connected: {socketId} Total online users: {count}"
onlineUsers
Direction: Server → Client (broadcast)
Emitted when:
- A user connects
- A user disconnects
Disconnection
disconnect
Direction: Client → Server (automatic)
Triggered when:
- User closes browser/tab
- Network connection lost
- Client explicitly calls
socket.disconnect() - Server terminates connection
| Resource | Action |
|---|---|
onlineUsers | Decremented (min: 0) |
authenticatedUsers | Removed from Map |
waitingUsers | Removed from array |
videoEnabledUsers | Removed from Set |
rtcReadyUsers | Removed from Set |
chatPairs | Cleaned up for user and partner |
| Partner notification | partnerLeft event sent |
partnerLeft- Sent to chat partner when user disconnects during active chat
Complete Connection Lifecycle Example
Security Considerations
- JWT Token Security: Store tokens securely (HttpOnly cookies or secure localStorage)
- Token Expiration: Handle token expiration gracefully with refresh logic
- CORS Configuration: Only allowed origins can connect
- Socket ID Exposure: Socket IDs are shared with chat partners (not sensitive)
- Rate Limiting: Consider implementing rate limiting for production use
Related Events
- findChat - Start looking for a chat partner after connection
- partnerLeft - Sent when disconnect occurs during active chat