Reputation: 399
I'm new with node.js and socket.io
I have two clients, and they have their ID 1 & 2. I need to send data via socket.io that contain: user_from, user_to, action
And the data is send from both sides.
How do i send this data from client 1 with socket.emit(), so I can set variable:data ?
How to read that data on server with socket.get ?
And send it again from server to client 2 ? (i found this code: io.sockets.volatile.emit( 'broadcast_msg' , msg ); but i want to send it only from specific user to specific user, not all users connected.)
Thank you for your help, I see a lot of examples, but not what I need.
UPDATE:
I don't understand this code:
// on server started we can load our client.html page
function handler(req, res) {
fs.readFile(__dirname + '/client.html', function(err, data) {
if(err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client.html');
}
res.writeHead(200);
res.end(data);
});
}
Why do I need to do this? I have ex: index.php?menu=30 or index.php?menu=30&action=12
Upvotes: 2
Views: 1608
Reputation: 925
You are looking for routing functionality. What socket.io provides is point-to-point communication. e.g. from 1 client to the server.
So logically, you need the server to route messages, which means that messages should have addressing information (e.g. ID of the target recipient). This can then be used to route messages by, for example, creating (custom) user-specific events to be triggered based on the target user of the incoming message.
If you are building anything that needs to scale, perhaps you should look at a messaging framework like RabbitMQ. It is exactly meant to route messages between distributed entities (like the users).
Cheers!
Upvotes: 1