0

I'm having trouble setting up signaling for namespaced socket.io rooms in node.js.

It seems that certain methods are not available outside of the 'root' namespace.

app.js:

// I'll skip the server setup
var io = require('socket.io').listen(server); 
var foo = require("./foo")(io.of('/foo'));
io.of('/foo').on('connection', function (socket) {
    try { foo.listen(socket); } catch(err) { console.log(err); }
});

foo.js:

module.exports = function(io){
    var signal = function(data){
        io.sockets.in('12345').emit('bar', data);
    };

    var listen = function(socket){
        socket.join('12345'); // no problem
        signal('test'); // woops!
    };
    return {listen: listen};
};

The problem occurs at io.sockets.in('12345'). The namespaced io has no in method:

TypeError: Object #<Object> has no method 'in'

Is there any way to broadcast to a namespaced room without using socket.broadcast?

Greg
  • 7,782
  • 7
  • 43
  • 69
  • http://stackoverflow.com/questions/6477770/socket-io-how-to-broadcast-messages-on-a-namespace – Damodaran Jan 02 '14 at 08:02
  • @Damodaran - unfortunately, it looks like that solution calls for `socket.broadcast` and I would like to keep this out of the individual sockets' event listeners – Greg Jan 02 '14 at 08:14

1 Answers1

0

Figured it out.

Because the variable passed as the io parameter to the module.exports of foo.js is already namespaced (it receives io.of('/foo')), there is no need to for the sockets in io.sockets.in. That io is already itself the sockets object.

In other words, I would just use:

var signal = function(data){
    io.in('12345').emit('bar', data);
};

... and it works perfectly.

Greg
  • 7,782
  • 7
  • 43
  • 69