Reputation: 5919
I have a question about how SocketIO works.
If I emit a socket to "room1":
socketio.sockets.to(room1).emit('chat.message',data);
And this room1 have registered 10000 sessions inside...
Is it the same that iterate this code for 10000 times? :
socketio.sockets.to(sessionId).emit('chat.message',data);
Maybe is worst in efficiency?
Emitting to a room, internally the socketIO is sending the message to all sessions registered in the room? Or how SocketIO works?
This question is because I have a room that I need to emit a message only to part of this room, and also I have micro-rooms with userIds, and maybe is more complicated to create an another room that iterate the socket.emit
and send to all micro-rooms that I needed to emit this message.
Upvotes: 0
Views: 315
Reputation: 2832
Whenever any client connected with server, it will be joined to room automatically with roomId (some random string). And when you make explicitly join to another room, then socket will be connected to both room with roomId (whatever you specified).
socketio.sockets.to(sessionId).emit('chat.message', data);
socketio.sockets.to(room1).emit('chat.message', data);
Both statements do the same thing. So emit will be broadcast message to room. Room (sessionId) has only one user and Room (room1) can have multiple users connected to it.
Internally it is calling socket.io-adpater's broadcast method.
https://github.com/socketio/socket.io-adapter/blob/master/index.js Line : 111
And it is iterating each connected client and sending the data.
Upvotes: 1