Socket.io: Sockets not joining rooms

Created on 14 Sep 2018  路  11Comments  路  Source: socketio/socket.io

You want to:

  • [x] report a bug
  • [ ] request a feature

Current behaviour

After updating my socket.io from 1.x to ^2.1.1 my sockets are not joining any rooms anymore.

Steps to reproduce (if the current behaviour is a bug)

async socketJoinRoom(socket: any, room: string, userId: number) {
        await socket.join(room)

        // tried this to log all the clients in the room, the room ID is correct but "clients" array is empty
        this.io.in(room).clients((err, clients) => {
            console.log(room)  // loggs the correct room
            console.log(clients) // loggs an empty array
        })
        catSystem.info("User "+userId+" joined socket room '"+room+"'")
    }

also this is not working:

  this.io.in('myroom').emit('test', {})

Expected behaviour

I expected to get a list of sockets in the room and that messages are emitted to the room.

Note: Emitting to a single socket works fine.

Setup

  • OS: Ubuntu 18.04
  • browser: Chrome Version 69.0.3497.81 (64 Bit)
  • socket.io version: " ^2.1.1"
  • NodeJs version v10.9.0

Any suggestions here?

Most helpful comment

Will this be fixed soon? My Projects depends largely on socket rooms....So if I cannot use them with socket.io anymore i need to refactor my code soon to use pure WebsocketApi.....please answer

Are You using a namespace? I faced this same problem as stated above and was able to make mine work by changing

io.to.(room)

to

io.of(namespace).to(room)

All 11 comments

i also face this problem

Will this be fixed soon? My Projects depends largely on socket rooms....So if I cannot use them with socket.io anymore i need to refactor my code soon to use pure WebsocketApi.....please answer

I believe that I'm having a similar issue,

I am currently using Socket.Io (version 2.1.1) on my Node.js server and trying to use the functionality for joining and emitting messages & events to particular rooms in a namespace. On the server side, my code looks like this:

` io
       .of("/chat")
       .on("connection", (socket: any) => {
               socket.join('room1', () => {
                   console.log(socket.rooms);
                   io.sockets.in("room1").emit('connectedToRoom1', { message: "You connected room 1" });
               });
       }); `

I know that I am successfully connecting to room1 because it logs the following response to the console when I invoke:

`
console.log(socket.rooms); // this successfully logs that I joined room1

// The response
   { '/chat#LMPBwLXoBonQ11XDAAAB': '/chat#LMPBwLXoBonQ11XDAAAB',
     room1: 'room1' }

On the client side, my code looks like this:

var socket = io.connect("http://localhost:3000/chat");

socket.on("connectedToRoom1", () => {
           console.log("A new user has joined room1");
       }); `

My problem is that the client side does not log to the console, it appears to me that the client side is simply not responding to the even that is being listened for. I can correctly respond to and emit normal events in Socket.Io (like the original poster said, emitting to a single socket is fine) in my current set up, it simply won't let me do this with rooms. Is there something that I am doing wrong here?

Thanks in advance, Michael

You want to:

  • [x] report a _bug_
  • [ ] request a _feature_

Current behaviour

After updating my socket.io from 1.x to ^2.1.1 my sockets are not joining any rooms anymore.

Steps to reproduce (if the current behaviour is a bug)

async socketJoinRoom(socket: any, room: string, userId: number) {
        await socket.join(room)

        // tried this to log all the clients in the room, the room ID is correct but "clients" array is empty
        this.io.in(room).clients((err, clients) => {
            console.log(room)  // loggs the correct room
            console.log(clients) // loggs an empty array
        })
        catSystem.info("User "+userId+" joined socket room '"+room+"'")
    }

also this is not working:

  this.io.in('myroom').emit('test', {})

Expected behaviour

I expected to get a list of sockets in the room and that messages are emitted to the room.

Note: Emitting to a single socket works fine.

Setup

  • OS: Ubuntu 18.04
  • browser: Chrome Version 69.0.3497.81 (64 Bit)
  • socket.io version: " ^2.1.1"
  • NodeJs version v10.9.0

Any suggestions here?

Are You using a namespace? I faced this same problem as stated above and was able to make mine work by changing

io.to.(room)

to

io.of(namespace).to(room)

Will this be fixed soon? My Projects depends largely on socket rooms....So if I cannot use them with socket.io anymore i need to refactor my code soon to use pure WebsocketApi.....please answer

Are You using a namespace? I faced this same problem as stated above and was able to make mine work by changing

io.to.(room)

to

io.of(namespace).to(room)

I am facing the same issue, sometime socket able to join the room sometime it doesn't ... any update?

I faced same issue

Same here, with a namespace io.of(...).to(...).emit(). Works fine without the to(...).

I believe that I'm having a similar issue,

I am currently using Socket.Io (version 2.1.1) on my Node.js server and trying to use the functionality for joining and emitting messages & events to particular rooms in a namespace. On the server side, my code looks like this:

` io
       .of("/chat")
       .on("connection", (socket: any) => {
               socket.join('room1', () => {
                   console.log(socket.rooms);
                   io.sockets.in("room1").emit('connectedToRoom1', { message: "You connected room 1" });
               });
       }); `

I know that I am successfully connecting to room1 because it logs the following response to the console when I invoke:

`
console.log(socket.rooms); // this successfully logs that I joined room1

// The response
   { '/chat#LMPBwLXoBonQ11XDAAAB': '/chat#LMPBwLXoBonQ11XDAAAB',
     room1: 'room1' }

On the client side, my code looks like this:

var socket = io.connect("http://localhost:3000/chat");

socket.on("connectedToRoom1", () => {
           console.log("A new user has joined room1");
       }); `

My problem is that the client side does not log to the console, it appears to me that the client side is simply not responding to the even that is being listened for. I can correctly respond to and emit normal events in Socket.Io (like the original poster said, emitting to a single socket is fine) in my current set up, it simply won't let me do this with rooms. Is there something that I am doing wrong here?

Thanks in advance, Michael

Thanks It is working for me

/* join user room by room name */
user.on('join_room',function(room){
user.join(room); /// joi room code
console.log("Success Join Room of "+room);
});

Will this be fixed soon? My Projects depends largely on socket rooms....So if I cannot use them with socket.io anymore i need to refactor my code soon to use pure WebsocketApi.....please answer

Are You using a namespace? I faced this same problem as stated above and was able to make mine work by changing

io.to.(room)

to

io.of(namespace).to(room)

you saved my day!

Was this page helpful?
0 / 5 - 0 ratings