Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Channels in Socket.io

I am trying to broadcast a message through the Node.js service socket.io (http://socket.io/) to certain subset of all subscribers.

To be more exact, I would like to use channels that users can subscribe to, in order to efficiently push messages to a couple hundred people at the same time.

I'm not really sure if addEvent('channel_name',x) is the way to go.

I have not found anything in the docs. Any ideas?

Thanks Mat

like image 983
mat3001 Avatar asked Oct 14 '22 05:10

mat3001


1 Answers

This is a little late(!) but I spotted it in my referrer logs (I'm the author of Faye). It's easy to publish messages from your application whether it's in the same server as Faye or not. For example:

var faye = require('faye'),
    http = require('http');

// Set up the server

var server = http.createServer(function(req, res) {
  // dispatch to your app logic...
});

var bayeux = new faye.NodeAdapter({mount: '/bayeux'});
bayeux.attach(server);
server.listen(8000);

If your app logic is in the same server process you can do this:

bayeux.getClient().publish('/channel', {hello: 'world'});

Otherwise you can make a client in Node that connects to your Faye server:

var client = new faye.Client('http://0.0.0.0:8000/bayeux');
client.publish('/channel', {hello: 'world'});

Either way, the Faye server will relay the message to any subscribed clients whether they are server- or client-side. Hope that helps.

like image 116
jcoglan Avatar answered Oct 30 '22 02:10

jcoglan