Browse Source

duplicate user fix

main
Sourya Banerjee 3 months ago
parent
commit
773e175288
1 changed files with 10 additions and 0 deletions
  1. +10
    -0
      src/index.ts

+ 10
- 0
src/index.ts View File

@ -37,6 +37,16 @@ io.on("connection", (socket: Socket) => {
// Handle user-online event // Handle user-online event
socket.on("user-online", (user: any) => { socket.on("user-online", (user: any) => {
// console.log("User online:", user);
// Check if the same user exists in the map
for (const [key, value] of onlineUsers.entries()) {
console.log("value", value);
if (value.userId === user.userId) {
// Assuming user object has an 'id' property
onlineUsers.delete(key);
break;
}
}
// Now we store the socket ID along with the user details // Now we store the socket ID along with the user details
onlineUsers.set(socket.id, { ...user, socketId: socket.id }); onlineUsers.set(socket.id, { ...user, socketId: socket.id });
io.emit("online-users", Array.from(onlineUsers.values())); io.emit("online-users", Array.from(onlineUsers.values()));


Loading…
Cancel
Save