MrGlass MrGlass - 5 months ago 16
Node.js Question

socket.io client to client messaging

I'm having trouble getting basic client to client (or really client->server->client) working with socket.io. Heres the code I have right now:

io.sockets.on('connection', function (socket) {

users.push(socket.sessionId);

for(userID in users) {
console.log(userID);
io.sockets.socket(userID).emit('message', { msg: 'New User Connected succesfully' });
}
socket.emit('message', { msg: 'Connected succesfully' });


socket.on('my other event', function (data) {
console.log(data);
});
});


From my understanding, that should send the new user message to every connected user (individually, since i want to do actual individual messages later). Instead, I only get the 'connected successfully' message at the end. I don't get any errors or other negative indicators from my server or client.

Any ideas of why
io.sockets.socket(userID).emit()
doesn't work or what to use in its place?

mak mak
Answer

Try

users.push(socket); // without .sessionId

for (var u in users)    {
   // users[u] is now the socket
   console.log(users[u].id);
   users[u].emit('message', { msg: 'New User Connected succesfully' });
}