Good afternoon, I do chat on nodejs and socket.io. How to get the number of users who are logged in and are online in the chat. I just need to take into account that the user can log in from different devices at the same time, but the number should show exactly how many have logged in. I do the standard:
var connect_user = 0; //количество людей var users_chat = []; //массив var on_user = users_chat.indexOf(socket.handshake.session.passport.user.user_id); //проверяю id user if (on_user == -1) { //если id нет в массиве, то его добавляю var user_id = socket.handshake.session.passport.user.user_id; users_chat.push(user_id); io.sockets.emit('user_left', { connect_user: connect_user + 1 }); socket.broadcast.emit('message', { send: { time: (new Date).toLocaleTimeString(), nickname: 'admin', message: 'Кто-то зашёл в чат.' } }); } else { //иначе оставляю, как есть io.sockets.emit('user_left', { connect_user: connect_user }); } I can’t implement user exit. I do this:
var off_user = connect_user.indexOf(socket.handshake.session.passport.user.user_id); if (off_user != -1) { connect_user.splice(off_user, 1); } If the user is logged out from one device, the number of users decreases. And I need to do a check.