SocketIO groups

I am trying SocketIO and I am stuck. I can't find any suitable documentation.

Here is some sample code of what I would like to do:

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

      

Is there any workaround for these two specific steps?

+3


source to share


2 answers


The io.sockets.clients function is what I need. This allows me to move clients of a particular room, add them to a group if necessary, or register an event.



+1


source


What you need to do is join () the 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 the documentation at https://github.com/LearnBoost/socket.io (search for rooms).

+8


source







All Articles