Created
February 11, 2013 21:47
-
-
Save jazlalli/4757900 to your computer and use it in GitHub Desktop.
message router - binds event handlers and emits event on message
This file contains hidden or 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 config = require('../config_' + environment), | |
routes = require('./routes'), | |
EventEmitter = require('events').EventEmitter, | |
messagerouter = {}, | |
topic; | |
var emitter = new EventEmitter(); | |
for (topic in routes) { | |
if (routes.hasOwnProperty(topic)) { | |
if (typeof (routes[topic]) === 'function') { | |
emitter.addListener(topic, routes[topic]); | |
} | |
} | |
} | |
messagerouter.route = function (message, deliveryinfo) { | |
for (topic in routes) { | |
if (routes.hasOwnProperty(topic) && topic === deliveryinfo.routingKey) { | |
emitter.emit(deliveryinfo.routingKey, message, function () { | |
console.log(deliveryinfo.routingKey + ' message received'); | |
}); | |
} | |
} | |
} | |
module.exports = messagerouter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment