Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You would probably like to try Push-it: <a href="http://github.com/aaronblohowiak/Push-It" rel="noreferrer">http://github.com/aaronblohowiak/Push-It</a> which is built on top of Socket.IO. Design adheres to the Bayeux Protocol.</p> <p>However, if you need something that uses redis pubsub you can check <a href="http://github.com/shripadk/Socket.IO-PubSub" rel="noreferrer">http://github.com/shripadk/Socket.IO-PubSub</a></p> <p>Specifically answering your question: You can maintain an array of all the clients connected to the websocket server. And probably just broadcast to a subset of those clients? The broadcast method does essentially that under the hood. node-websocket-server/Socket.IO maintains an array of all the clients connected and just loops through all of them "send"ing a message to each of the clients. Gist of the code:</p> <pre><code>// considering you storing all your clients in an array, should be doing this on connection: clients.push(client) // loop through that array to send to each client Client.prototype.broadcast = function(msg, except) { for(var i in clients) { if(clients[i].sessionId !== except) { clients[i].send({message: msg}); } } } </code></pre> <p>So if you want to relay messages only to specific channels, just maintain a list of all the channels subscribed by the client. Here is a simple example (to just get you started) :</p> <pre><code>clients.push(client); Client.prototype.subscribe = function(channel) { this.channel = channel; } Client.prototype.unsubscribe = function(channel) { this.channel = null; } Client.prototype.publish = function(channel, msg) { for(var i in clients) { if(clients[i].channel === channel) { clients[i].send({message: msg}); } } } </code></pre> <p>To make it even easier use EventEmitters. So in node-websocket-server/Socket.IO see where the messages are being received and parse the message to check the type (subscribe/unsubscribe/publish) and emit the event with the message depending on the type. Example:</p> <pre><code>Client.prototype._onMessage = function(message) { switch(message.type) { case 'subscribe': this.emit('subscribe', message.channel); case 'unsubscribe': this.emit('unsubscribe', message.channel); case 'publish': this.emit('publish', message.channel, message.data); default: } } </code></pre> <p>Listen to the events emitted in your app's on('connection') :</p> <pre><code>client.on('subscribe', function(channel) { // do some checks here if u like client.subscribe(channel); }); client.on('unsubscribe', function(channel) { client.unsubscribe(channel); }); client.on('publish', function(channel, message) { client.publish(channel, message); }); </code></pre> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload