Inside the disconnect listener, we want to remove the user and then we want to update the list once again. I'm going to do that by doing a few separate things. First up, we're going to make a variable called user, storing any potentially removed users, remember the removeUser method does return the user removed, users.removeUser passing in the ID, socket.id:
socket.io('disconnect', () => {
var user = users.removeUser(socket.id);
});
Now we only want to do something if we actually removed a user, if the person hadn't joined a room, there's no reason to actually do anything. If a user was removed we are going to emit two events, and we're going to emit them to every single person connected to the chatroom, which means that we're going to be using io.to().emit, just like we did in the preceding code. We're going to do this two times, so I'm going to copy this line and paste it, like this:
socket.io('disconnect', () => {
var user = users.removeUser(socket.id);
if (user){
io.to().emit();
io.to().emit();
}
});