socket io best setup code example
Example 1: how to check whether the user is online or not using socket.io
const users = {};
io.on('connection', function(socket){
console.log('a user connected');
socket.on('login', function(data){
console.log('a user ' + data.userId + ' connected');
users[socket.id] = data.userId;
});
socket.on('disconnect', function(){
console.log('user ' + users[socket.id] + ' disconnected');
delete users[socket.id];
});
});
Example 2: simple socket.io chat
npm install socket.io