Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Created February 11, 2013 21:47
Show Gist options
  • Save jazlalli/4757900 to your computer and use it in GitHub Desktop.
Save jazlalli/4757900 to your computer and use it in GitHub Desktop.
message router - binds event handlers and emits event on message
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