Repository navigation
Reading 18
Socket.IO allows you to “namespace” your sockets, which essentially means assigning different endpoints or paths.
This is a useful feature to minimize the number of resources (TCP connections) and at the same time separate concerns within your application by introducing separation between communication channels.
Default namespace We call the default namespace / and it’s the one Socket.IO clients connect to by default, and the one the server listens to by default.
This namespace is identified by io.sockets or simply io:
// the following two will emit to all the sockets connected to /
io.sockets.emit('hi', 'everyone');
io.emit('hi', 'everyone'); // short form
Each namespace emits a connection event that receives each Socket instance as a parameter
io.on('connection', function(socket){ socket.on('disconnect', function(){ }); });
Custom namespaces To set up a custom namespace, you can call the of function on the server-side:
const nsp = io.of('/my-namespace'); nsp.on('connection', function(socket){ console.log('someone connected'); }); nsp.emit('hi', 'everyone!'); On the client side, you tell Socket.IO client to connect to that namespace:
const socket = io('/my-namespace');
Sending messages from the outside-world In some cases, you might want to emit events to sockets in Socket.IO namespaces / rooms from outside the context of your Socket.IO processes.
There’s several ways to tackle this problem, like implementing your own channel to send messages into the process.
To facilitate this use case, we created two modules:
socket.io-redis socket.io-emitter By implementing the Redis Adapter:
const io = require('socket.io')(3000); const redis = require('socket.io-redis'); io.adapter(redis({ host: 'localhost', port: 6379 })); you can then emit messages from any other process to any channel
const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 }); setInterval(function(){ io.emit('time', new Date); }, 5000);