diff --git a/src/index.ts b/src/index.ts index 6e55d2c..35455f1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -37,6 +37,16 @@ io.on("connection", (socket: Socket) => { // Handle user-online event 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 onlineUsers.set(socket.id, { ...user, socketId: socket.id }); io.emit("online-users", Array.from(onlineUsers.values()));