SocketIO groups

Go To StackoverFlow.com

3

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 ?

2012-04-05 16:37
by Alexandre Kirszenberg


1

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.

2012-04-26 17:50
by Alexandre Kirszenberg


8

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).

2012-04-06 09:29
by diversario
Ads