What is the difference between Socket.io namespaces and rooms?
Namespaces are top-level channels that separate features (chat, admin, notifications), each with its own auth and events. Rooms are sub-groups within a namespace for grouping users (chat rooms, group chats). Use namespaces for feature separation, rooms for user grouping.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Socket.io Namespaces and Rooms Organizing Connections in Node.js
Use io.of('/namespace-name'): const adminNsp = io.of('/admin'). Add namespace-specific middleware with adminNsp.use() and handle events with adminNsp.on('connection', ...). The client connects to the namespace with io('url/admin', { auth: { token } }).
Use namespaces to separate features with different auth requirements or event sets: chat (default /), admin dashboard (/admin with admin-only auth), notifications (/notifications for push notifications), live updates (/live for data feeds). Each namespace has its own middleware and event handlers.
When the user connects, join them to a personal room: socket.join('user:' + userId). To send a notification from a route handler: io.of('/notifications').to('user:' + userId).emit('notification', data). Only that user receives the notification.
Still have questions?
Browse all our FAQs or reach out to our support team
