Created
February 11, 2013 21:59
-
-
Save jazlalli/4757988 to your computer and use it in GitHub Desktop.
server.js - connects to amqp and registers router.route as the subscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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