Created
March 30, 2011 11:04
-
-
Save 3rd-Eden/894207 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(constructor){ | |
var io = constructor.Listener.prototype; | |
/** | |
* Publishes messages over variouse of channels | |
* | |
* @param {Socket.IO.Client} current The client that sends the message | |
* @param {Array} rooms The rooms that receive the message | |
* @param {Mixed} message The message for the channel | |
* @returns {Socket.IO.Listener} | |
* @api public | |
*/ | |
io.publish = function(current, rooms, message){ | |
var keys = Object.keys( this.clients ) | |
, i = keys.length | |
, client; | |
while( i-- ){ | |
// reduce lookups | |
client = this.clients[ keys[i] ]; | |
if( client === current ) continue; | |
if (client && client.rooms && client.rooms.some(function(room){ | |
return rooms.indexOf(room) !== -1 | |
})) | |
{ | |
client.send(message); | |
} | |
} | |
return this; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And you can call the module:
var io = require('socket.io'); require("./extend.io")(io);
Then
var socket = io.listen(app);
and you can broadcast to a subset of channels:
socket.publish(client, ['name','of','rooms'], "Hello users in my rooms")
complete example implementation: