Skip to content

Instantly share code, notes, and snippets.

@jcoglan
Created October 11, 2010 22:23
Show Gist options
  • Save jcoglan/621332 to your computer and use it in GitHub Desktop.
Save jcoglan/621332 to your computer and use it in GitHub Desktop.
var faye = require('faye'),
sys = require('sys');
var client = new faye.Client('http://127.0.0.1:8000/faye');
client.subscribe('/messages', function(message) {
// This should never run due to server-side extension
sys.puts(sys.inspect(message));
});
setInterval(function() {
client.publish('/messages', {hello: 'world'});
}, 1000);
var http = require('http'),
sys = require('sys'),
faye = require('faye');
var serverProtection = {
incoming: function(message, callback) {
if (message.channel !== '/meta/subscribe' || message.subscription !== '/messages')
return callback(message);
if (!message.ext || message.ext.authToken !== 'whatever')
message.error = 'I need an authToken!';
callback(message);
}
};
var clientAuthentication = {
outgoing: function(message, callback) {
if (message.channel !== '/meta/subscribe' || message.subscription !== '/messages')
return callback(message);
message.ext = message.ext || {};
message.ext.authToken = 'whatever';
callback(message);
}
};
var server = http.createServer(),
bayeux = new faye.NodeAdapter({mount: '/faye', timeout: 45});
bayeux.addExtension(serverProtection);
bayeux.attach(server);
server.listen(8000);
var client = bayeux.getClient();
client.addExtension(clientAuthentication);
client.subscribe('/messages', function(message) {
sys.puts(sys.inspect(message));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment