Brian Yeh Brian Yeh - 1 year ago 279
Node.js Question

How to send data to specific client in a specific room with socket io

My question is what's the proper way of sending data to a specific client in a specific room. I'm using socket io and the code written below:

I use the command:'change', {data})

but the client never gets this command. Anyone know why?

Below is a snippet of my code:

server code:

io.on('connection', function (socket) {
socket.on('channelJoin', function(channel){

console.log("sending initial data to: ";'change', deltasByChannel[channel]);

socket.on("change", function(delta){
console.log("channel: " + channel+" was edited!");
deltasByChannel[channel] ? deltasByChannel[channel] = deltasByChannel[channel].concat(delta) : deltasByChannel[channel] = delta;'change', delta);

http.listen(3000, function () {
console.log('listening on *:3000');

client code:

var channel = window.location.pathname;
var socket = io.connect();

//Ace handlers
var sendUpdateData = function(e){
socket.emit("change", []);

socket.on('connect', function(){
socket.on("change", function(data){
console.log("change event received!");
editor.getSession().removeListener('change', sendUpdateData);
editor.getSession().on('change', sendUpdateData);
editor.getSession().on('change', sendUpdateData);

socket.emit('channelJoin', channel);

just to avoid confusion the editor object is listening for a change event as well. It's from an entirely different library (ace.js) that has nothing to do with my socket io issue.

below is another snippet of the server code for more clarity:

var http = require('http').Server(app);
var io = require('')(http);

http.listen(3000, function () {
console.log('listening on *:3000');

Answer Source

I think there's some confusion about sending data over sockets using You can elect to emit events or data using rooms or private namesspaces, you can broadcast to all connected sockets, or you can emit data to a specific ID.

In your case you should just be selecting a, to emit an event to a particular connection. You can do this by:

io.sockets.connected[ ].emit('privateMsg', 'hello this is a private msg');

You can also use the to() method in conjunction with broadcast as well: ).emit('privateMsg', 'hello this is a private msg');

This will reach the user which matches the you pass in as the argument.

To contact users within a "room" or private namespace, you can also use the to() method:'some room').emit('some event');

In this case some room would be the channel var you've defined, and it should match a predefined variable that has already been instantiated.

For more information about rooms/namespaces/and reaching specific socket connections:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download