开发者

Socket.IO room feature

开发者 https://www.devze.com 2023-04-11 02:59 出处:网络
I have two separate files that one is server-side JS. The other one is dynamically generated client-side PHP.

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 be开发者_JAVA百科low 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.


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!


I have not used Socket.IO in quite some time, but

1.) As the documentation states: "Broadcasting means sending a message to everyone else except for the socket that starts it." After some searching with the Socket.IO code, you can take a look at Socket.prototye.packet, in /lib/socket.js.

When you do your first call, you are setting the user to the "justin bieber fans" room, and broadcasting "new fan" for everyone but the socket. In the second instance, you are broadcasting for everyone in 'rammstein fans' to send 'new non-fan'.

2.) In the first call, you are emitting 'event' just for the single user, and the message to that single room.

In the second call, you are sending for everyone in that room.

3.) Once again, looking at the source code, they both look pretty similar, except that socket.io protocol accepts 3 different kind of messages: "event", "json", "message". With .send, you can send a json string, or a message. With .emit, you are going to send an event to the client. It does not make much a difference, they are almost handled the same internally, but you should use the one that best fits your needs.


I'm using room feature well with RedisStore.

As you can see in the Socket.io source files.

Room feature ( Redis' subscribe and unsubscribe is used to implement that feature) seems

to be only available with using RedisStore but not with the default memoryStore.

I have found

{}

empty source for join and leave on memoryStore.

So I recommend using RedisStore who would like to cluster the Socket.io conenctions

between multiple process and multiple server

and also for the successful using of room feature of Socket.io

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号