Si può provare questo
io.sockets.on('connection', function (socket) {
socket.on('event_name', function(data) {
// you can try one of these three options
// this is used to send to all connecting sockets
io.sockets.emit('eventToClient', { id: userid, name: username });
// this is used to send to all connecting sockets except the sending one
socket.broadcast.emit('eventToClient',{ id: userid, name: username });
// this is used to the sending one
socket.emit('eventToClient',{ id: userid, name: username });
}
}
e sul client
socket.on('eventToClient',function(data) {
// do something with data
var id = data.id
var name = data.username
});
fonte
2013-12-17 11:14:26