How do I broadcast within a namespace?
Use io.of('/namespace').emit() for all sockets in the namespace, io.of('/namespace').to('room').emit() for a specific room, or io.of('/namespace').except('room').emit() to exclude a room. Each namespace's broadcasts only affect sockets in that namespace.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Socket.io Namespaces and Rooms Organizing Connections in Node.js
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.
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.
Still have questions?
Browse all our FAQs or reach out to our support team
