SocketIO groups

Problem

I'm trying SocketIO and I'm stuck. I can't find any proper documentation.

Here is a sample code of what I'd like to do :

io.sockets.in('group1').join('group2');
io.sockets.in('group3').on('message', function(){});

Is there any workaround to those two particular actions ? Thanks !

Problem courtesy of: Alexandre Kirszenberg

Solution

The function io.sockets.clients is what I needed. It allows me to traverse the clients of a specific room, adding them to a group if necessary, or registering an event.

Solution courtesy of: Alexandre Kirszenberg

Discussion

What you need to do is join() a socket into a group:

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.join('justin bieber fans'); // put socket in a channel
  socket.broadcast.to('justin bieber fans').emit('new fan'); // broadcast a message to a channel
  io.sockets.in('rammstein fans').emit('new non-fan'); // to another channel
});

You need to do this for every socket that connects to you.

This is from documentation at https://github.com/LearnBoost/socket.io (search for Rooms).

Discussion courtesy of: diversario

This recipe can be found in it's original form on Stack Over Flow.