Socket.io: I'm still getting messages even after leave the room.

Created on 22 Jul 2016  路  11Comments  路  Source: socketio/socket.io

Hi all

I'm still getting messages even after leave the room.

Here is the server code.

io.sockets.on( 'connection', function( socket ) {
  console.log('received connection signal');

  //join chat room
  socket.on('join', function(user, room) {
      socket.join(room);
      socket.user = user;
      socket.room = room;
      console.log(user + " Joined the room: "+ room);

  });

  //leave the chat room
  socket.on('leave', function(user, room) {
      socket.room = '';
      console.log(user + " left the room: "+ room);
      socket.broadcast.to(room).emit('disconnected', user, room, 'disconnected');
      socket.leave(room);

  });
});

server.listen(port, function() {
  console.log('Express server listening on %d', port)
});

Can you point me the issue?

Thanks.

Most helpful comment

You should use the callback argument of leave:

socket.leave(room, function(){
  socket.broadcast...
});

All 11 comments

@socketio can you guys answer this?

Maybe if you post the code that is sending the messages it could procide some additional info.

You should use the callback argument of leave:

socket.leave(room, function(){
  socket.broadcast...
});

@GGAlanSmithee Here is the code from front-end.

$scope.$on('chat:leaveroom', function() {
        console.log('received a signal to leave chat.');
        var current_user = UserService.getUser();
        SocketService.emit('leave', current_user.facebook_id, GlobalService.chat_room);
        GlobalService.chat_room = '';
    });

@wiadev did that fix your issue?

I'm also having this issue on v1.5.0. When we switch to a new room, messages are sent to the previous room. Switching again, sent to the previous but not all 3. I check socket.rooms and it seems correct.

@EddieOne could you please provide a sample project reproducing the issue please?

Ah a coworker found my error. We have a room variable where we store the socket.io room id and it was not getting changed. I would not have guessed it's possible to send to another room which has not been joined.

@darrachequesne Thanks for the previous answer. it works great.

I have another question.
can we somehow delay the disconnection?
for example, while A and B was having a chat and A's iphone screen was locked by mistake.
but at this point, the socket server receives the disconnection request instantly and it makes the A drops the chatting room and chat was disconnected.

so can we delay receiving the disconnection request somehow like 1 min? so user can easily reconnect to the chat session without re-create.

Thanks.

Since that is more of a usage question, you might post on stackoverflow instead of the issues tracker.

As suggested by @EddieOne, please ask that question at stackoverflow. Thanks!

Was this page helpful?
0 / 5 - 0 ratings