рабочий пример
var clients = 0;
io.on('connection', (socket) => {
clients++;
console.log('connect: '+ clients);
socket.emit('news',{value: 'connect: '+ clients}); //to client
io.sockets.emit('news',{value: 'Hi all !'});// to all
socket.on('disconnect', () => {
clients--;
console.log('disconnect: '+ clients);
});
socket.on('error', (err) => {
console.log(err);
});
});