Skip to content

Commit

Permalink
Socket online status frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Keagan Aromin authored and Keagan Aromin committed Jan 12, 2025
1 parent 15b618c commit 5cafe5f
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 8 deletions.
15 changes: 13 additions & 2 deletions backend/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ const io = new Server(server, {
}
})

const userSocketMap = {}; // {userId: socketId}

io.on('connection', (socket) => {
console.log("user has connected", socket.id);

//socket.on is a listener for the events; can be used on both client and server side
//add user to socketmap when connecting
const userId = socket.handshake.query.userId;
if (userId != "undefined") userSocketMap[userId] = socket.id;

// io.emit() is an emitter for the connected clients; sends events to all connected clients
io.emit("getOnlineUsers", Object.keys(userSocketMap))
// socket.on is a listener for the events; can be used on both client and server side
socket.on('disconnect', () => {
console.log('user disconnected', socket.id)
console.log('user disconnected', socket.id);
//delete user from socketmap and update online users when disconnecting
delete userSocketMap[userId];
io.emit('getOnlineUsers', Object.keys(userSocketMap));
})
})
export {app, io, server}
Loading

0 comments on commit 5cafe5f

Please sign in to comment.