Rooms

Within each Namespace, you can define arbitrary channels called “Rooms” that sockets can join and leave.

This is useful to broadcast data to a subset of sockets:

Room diagram

Joining and leaving

You can call join to subscribe the socket to a given channel:

  1. io.on('connection', socket => {
  2. socket.join('some room');
  3. });

And then simply use to or in (they are the same) when broadcasting or emitting:

  1. io.to('some room').emit('some event');

You can emit to several rooms at the same time:

  1. io.to('room1').to('room2').to('room3').emit('some event');

In that case, an union) is performed: every socket that is at least in one of the rooms will get the event once (even if the socket is in two or more rooms).

You can also broadcast to a room from a given socket:

  1. io.on('connection', function(socket){
  2. socket.to('some room').emit('some event');
  3. });

In that case, every socket in the room excluding the sender will get the event.

To leave a channel you call leave in the same fashion as join. Both methods are asynchronous and accept a callback argument.

Default room

Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by its own id.

This makes it easy to broadcast messages to other sockets:

  1. io.on('connection', socket => {
  2. socket.on('say to someone', (id, msg) => {
  3. socket.to(id).emit('my message', msg);
  4. });
  5. });

Sample use cases

  • broadcast data to each device / tab of a given user
  1. io.on('connection', async (socket) => {
  2. const userId = await computeUserIdFromHeaders(socket);
  3. socket.join(userId);
  4. // and then later
  5. io.to(userId).emit('hi');
  6. });
  • send notifications about a given entity
  1. io.on('connection', async (socket) => {
  2. const projects = await fetchProjects(socket);
  3. projects.forEach(project => socket.join('project:' + project.id));
  4. socket.on('update project', async (payload) => {
  5. const project = await updateProject(payload);
  6. io.to('project:' + project.id).emit('project updated', project);
  7. });
  8. });

Disconnection

Upon disconnection, sockets leave all the channels they were part of automatically, and no special teardown is needed on your part.

You can fetch the rooms the Socket was in by listening to the disconnecting event:

  1. io.on('connection', socket => {
  2. socket.on('disconnecting', () => {
  3. const rooms = Object.keys(socket.rooms);
  4. // the rooms array contains at least the socket ID
  5. });
  6. socket.on('disconnect', () => {
  7. // socket.rooms === {}
  8. });
  9. });

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 are 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:

By implementing the Redis Adapter:

  1. const io = require('socket.io')(3000);
  2. const redis = require('socket.io-redis');
  3. io.adapter(redis({ host: 'localhost', port: 6379 }));

you can then emit messages from any other process to any channel

  1. const io = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379 });
  2. setInterval(function(){
  3. io.emit('time', new Date);
  4. }, 5000);