Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Created February 11, 2013 21:59
Show Gist options
  • Save jazlalli/4757988 to your computer and use it in GitHub Desktop.
Save jazlalli/4757988 to your computer and use it in GitHub Desktop.
server.js - connects to amqp and registers router.route as the subscriber
var environment = process.env.NODE_ENV || '';
var amqp = require('amqp'),
router = require('./routing/messagerouter'),
routes = require('./routing/routes'),
config = require('./config_' + environment);
var amqpconnection = amqp.createConnection({
url: config.CLOUD_AMQP
});
var exchange,
queue;
amqpconnection.addListener('ready', function () {
// connect to exchange
exchange = amqpconnection.exchange(config.EXCHANGE_NAME, options = {passive: 'true'});
exchange.on('open', function () {
// define queue and bind to the exchange for handled message topics only
queue = amqpconnection.queue(config.QUEUE_NAME, function (q) {
var topic;
for (topic in routes) {
if (routes.hasOwnProperty(topic)) {
q.bind(exchange, topic);
}
}
});
// define message subscriber
queue.subscribe(function (message, headers, deliveryinfo) {
router.route(message, deliveryinfo);
});
});
});
var port = process.env.PORT || 1337;
console.log('Server running on port ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment