I'm coding an application with node.js and socket.io where users can talk with each others in personal chat rooms. Everyone can have multiple opened chat rooms. When the user wants to exit a chat room, system has to remove every socket listener of the room.
websocket.on('createRoom', function(roomID) {
...
var room = generateRoom();
...
// Leaving room
$('#exitButton').on('click', function() {
// Removes
websocket.removeAllListeners('createRoom');
});
// User joins the room
websocket.on('main/roomJoin/'+roomID, function(username) {
alert(username + ' has joined the room');
});
...
websocket.on('chat/messageReceived/'+roomID, function(message) {
room.printMessage(message);
});
});
The problem is that removeAllListeners doesn't remove the inner listeners, so if another user enters the room after the other exits, he receives the alert.
Another way to do this is to place the listeners outside, but it's harder to manage the multiple rooms.
Thanks.