Skip to content

Emitting to single room when socket was joined via socket.join([rooms]) doesn't emit. #2905

Closed
@andrewgrewell

Description

@andrewgrewell

You want to:

  • report a bug

Current behaviour

Doing io.in(roomA).emit(...) will not emit to that room if the socket was joined via socket.join([roomA, roomB, ...])

Steps to reproduce

see: fiddle to reproduce
example:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions