Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO room feature

I have two separate files that one is server-side JS.

The other one is dynamically generated client-side PHP.

Those two files are successfully able to communicate each other through Socket.IO.

I understand that I can restrict namespace by using .of() but that cannot be used

to handle dynamically created chat rooms.

So I have decided to use both

.of('/chat') 

and the room feature

.join('room name') 

I could find server-side example and could not find client-side example with it.

Below is the only given server-side code snippet from Socket.IO github

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

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

1) I'm having trouble understanding below part.

socket.broadcast.to('justin bieber fans').emit('new fan');
io.sockets.in('rammstein fans').emit('new non-fan');

What is difference between those two?

2) why not using

socket.to('room name').emit('event')

instead of

io.sockets.in('room name').emit('new non-fan');

3) Lastly, I have found some documentation that using

.send()

instead of

.emit()

Somewhat .send() does not work for me and I want to know difference between those two.

Thanks and I apologize for multiple questions about Socket.IO.

like image 292
jwchang Avatar asked Oct 07 '11 02:10

jwchang


1 Answers

1)-

i) io.sockets.in().emit(): It emit/dispatch a custom event to all in x room. Ex.;

/* Send message to the room1. It broadcasts the data to all 
   the socket clients which are connected to the room1 */

io.sockets.in('room1').emit('function', {foo:bar
});

ii) socket.broadcast.to().emit(): I emit/dispatch a custom event to all except the sender in x room. Ex.;

io.sockets.on('connection', function (socket) {
    socket.on('function', function(data){

        /* Broadcast to room1 except the sender. In other word, 
            It broadcast all the socket clients which are connected 
            to the room1 except the sender */
        socket.broadcast.to('room1').emit('function', {foo:bar});

    }
}

2)- First one, you emit/dispatch a custom event to the single client (socket one). Second one, you emit/dispatch a custom event to all the clients in x room.

3)- Different between emit() and send();

socket.emit(): is a function which emit/dispatch a custom event with a data to whoever clients you want to. socket.emit() takes at least two parameters, first one is the custom event name, second and so on ones are the data you want to pass. Ex.;

socket.emit('addUser',{nickname:'John'});

And you probably need to register and listen this custom event by using socket.on(). Ex.:

socket.on('addUser',function(data){
    console.log(data.nickname); // it will return 'John'
}

socket.send(): is pretty much same as socket.emit() just this time it uses a default event name 'message'. so it takes just one parameter, data you want to pass. Ex.:

socket.send('hi');

And this time you register and listen the 'message' event name;

socket.on('message', function (data) {
  console.log(data);  // it will return 'hi'
})

Hope this will help!

like image 155
Gokhan Tank Avatar answered Nov 08 '22 17:11

Gokhan Tank