Skip to content

Instantly share code, notes, and snippets.

@ben-bradley
Created April 2, 2014 05:20
Show Gist options
  • Save ben-bradley/9928366 to your computer and use it in GitHub Desktop.
Save ben-bradley/9928366 to your computer and use it in GitHub Desktop.
Faye & Express integration
/* http://en.wikipedia.org/wiki/Varan */
var Faye = require('faye'),
express = require('express'),
ping = require('ping');
var bayeux = new Faye.NodeAdapter({ mount: '/faye', timeout: 45 }),
client = bayeux.getClient(),
app = express();
app.configure(function() {
app.use(express.bodyParser());
app.use(express.static(__dirname + '/ui'));
});
client.subscribe('/ping', function(msg) {
ping.sys.probe(msg.host, function(alive) {
msg.alive = alive;
client.publish('/pong', msg);
});
});
var server = app.listen(8000);
bayeux.attach(server);
<html>
<head>
<script src='js/lib/jquery-2.1.0.min.js'></script>
<script src='faye/client.js'></script>
</head>
<body>
<h1>Chat Client</h1>
<div id='messages'></div>
<input type='text' id='host' />
<input type='button' value='Ping' id='ping' />
<script type='text/javascript'>
$(function() {
var client = new Faye.Client('/faye', { timeout: 20 });
client.subscribe('/pong', function(message) {
$('#messages').append('<p>'+message.host+' is '+message.alive+'</p>');
});
var $host = $('#host');
$('#ping').on('click', function() {
client.publish('/ping', { host: $host.val() });
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment