socket.io private message

message, node.js, socket.io

Solution

No tutorial needed. The Socket.IO FAQ is pretty straightforward on this one:

socket.emit('news', { hello: 'world' });

EDIT: Folks are linking to this question when asking about how to get that `socket` object later. There is no need to. When a new client connects, save a reference to that socket on whatever object you're keeping your user information on. My comment from below:

In the top of your script somewhere, setup an object to hold your users' information.

`var connectedUsers = {}; `

In your `.on('connection')` function, add that socket to your new object. `connectedUsers[USER_NAME_HERE] = socket;` Then you can easily retrieve it later. `connectedUsers[USER_NAME_HERE].emit('something', 'something'); `

Problem

I've beeen scouring the Net with no luck. I'm trying to figure out how to send a private message from one user to another. There are lots of snippets, but I'm not sure about the client/server interaction. If I have the ID of the socket I want to send to, how do I send it to the server, and how do I ensure the server only sends the message to that one receiver socket? Is there a tutorial or walkthrough that anyone knows of?

Original source