Search code examples
node.jssails.jssails.io.js

how to omit in sails.js two or more sockets in sails.sockets.broadcast?


I need to know how to omit in sails.js two or more sockets in sails.sockets.broadcast? I tried this:

 function sendMessage(data){
   var socketIds = ['socketId1','socketId2'];
   sails.sockets.broadcast("room","event",data,socketIds);
   //sending data to ALL sockets in the room :/
 }

but it doesn't work.

I need know this because I need omit the sockets which belong to the same session. (example: session of user in computer browser and android browser)

somebody help?


Solution

  • There's nothing built-in that will do this for you, but broadcast is just a wrapper around emit anyway, so you can just roll your own by getting all of the socket IDs in the room you want to broadcast to, and omitting the IDs in your array.

    // Get all the IDs of the sockets subscribed to "room"
    var socketIds = sails.sockets.subscribers("room");
    // Remove the IDs you want to omit
    socketIds = _.difference(socketIds, ['socketId1','socketId2']);
    // Emit your event to the rest!
    sails.sockets.emit(socketIds, "event", data);