You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
io.on('connect', (socket) => {
console.log('client connected to root, socket id: ' + socket.id);
socket.on('disconnect', () => console.log('client disconnected from root, socket id: ' + socket.id));
var rooms = ['a', 'b', 'c'];
socket.join(rooms, (err) => {
// respond with the status of the joins
socket.emit('joined-rooms', {
status: err ? 'success' : 'failure',
error: err,
message: `${err ? 'failed to join' : 'joined'} rooms: a, b, c`
});
// send a message to each single room that we joined - this seems to fail
rooms.forEach((room) => {
console.log('emitting message to room', room);
io.in(room).emit('message', {
multiRoom: false,
message: `this message was sent to a single room '${room}'`
});
});
// send a message to all rooms that we joined - this will work
console.log('emitting message to all rooms', rooms);
io.in(rooms).emit('message', {
multiRoom: true,
message: `this message was sent to rooms '${rooms}'`
});
});
});
Expected behaviour
I would expect to be able to do socket.join([...multipleRooms]) and later io.in(singleRoom).emit(...)
Setup
socket.io version: 1.7.3 (both client and server)
Solution
For now I am just doing rooms.forEach(room => socket.join(room) and then I am able to emit to a single room
The text was updated successfully, but these errors were encountered:
andrewgrewell
changed the title
[BUG] Emitting to single room when socket was joined via socket.join([rooms]) doesn't emit.
Emitting to single room when socket was joined via socket.join([rooms]) doesn't emit.
Mar 29, 2017
You want to:
Current behaviour
Doing
io.in(roomA).emit(...)
will not emit to that room if the socket was joined viasocket.join([roomA, roomB, ...])
Steps to reproduce
see: fiddle to reproduce
example:
Expected behaviour
I would expect to be able to do
socket.join([...multipleRooms])
and laterio.in(singleRoom).emit(...)
Setup
Solution
For now I am just doing
rooms.forEach(room => socket.join(room)
and then I am able to emit to a single roomThe text was updated successfully, but these errors were encountered: