I'm not sure it is possible, but I really need that feature. Can I do something like:
client side
sio = io.connect(url);
sio.to('room1').on('update', function(roomName) {
console.log(roomName);
});
Which will be triggered whenever an update event is emitted to 'room1'?
server side:
sockets.to('room1').emit('update', 'room1');
I'm not sure it is possible, but I really need that feature. Can I do something like:
client side
sio = io.connect(url);
sio.to('room1').on('update', function(roomName) {
console.log(roomName);
});
Which will be triggered whenever an update event is emitted to 'room1'?
server side:
sockets.to('room1').emit('update', 'room1');
Share
Improve this question
asked Aug 8, 2014 at 2:23
lsharirlsharir
1211 gold badge1 silver badge5 bronze badges
1
- Did you get the solution? – Ashish Patel Commented Sep 19, 2020 at 14:09
2 Answers
Reset to default 8That, unfortunately, doesn't work just on the client side. But you can do it on the server side.
Server Setup:
sockets.on('connection', function (socket) {
socket.on('join', function (room) {
socket.join(room);
});
});
//...
sockets.to('room1').emit('update', 'room1');
Client:
sio.emit('join', 'room1');
sio.on('update', function (room) {
console.log(room);
});
The "easy" way is to make sure that your emits from the server include the room in the payload.
sockets.to(room).emit('update', {room: room, message: 'hello world!'})
Most probably you want to do the same on the client-side of things. So that the messages sent from the client to the server includes a room identifier so that the message can be routed correctly.
Or you implement name spaces.