Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Created February 11, 2013 21:59

Revisions

  1. jazlalli created this gist Feb 11, 2013.
    39 changes: 39 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    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);