Skip to content

Instantly share code, notes, and snippets.

@cbankston
Created January 28, 2016 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cbankston/0f4de329570529f6014a to your computer and use it in GitHub Desktop.
Save cbankston/0f4de329570529f6014a to your computer and use it in GitHub Desktop.
node/rabbit example
module.exports = function(rabbit, subscribeTo) {
return rabbit.configure({
connection: {
user: 'guest',
pass: 'guest',
server: [
process.env.RABBITMQ_HOSTNAME
],
port: 5672,
vhost: '%2f',
},
exchanges: [
{
name: 'example-events-x',
type: 'topic',
autoDelete: false
}
],
queues: [
{
name: 'events_worker',
autoDelete: false,
subscribe: subscribeTo === 'example-events'
}
],
bindings: [
{
exchange: 'example-events-x',
target: 'events_worker',
keys: ['#']
}
]
});
};
var rabbit = require('wascally');
require('config/rabbitmq')(rabbit);
module.exports = {
emit_user_logged_in: emit_user_logged_in,
emit_user_logged_out: emit_user_logged_out
};
function emit_user_logged_in(message) {
return announce('user_logged_in', message);
};
function emit_user_logged_out(message) {
return emit('user_logged_out', message);
};
function emit(routingKey, body) {
return rabbit.publish('example-events-x', {
routingKey: routingKey,
type: 'publisher.message',
body: body
});
}
var EventRepo = require('src/repos/event');
EventRepo.emit_user_logged_in({ some: 'thing', more: 'interesting' }).then(function() {
console.log('announced!');
});
EventRepo.emit_user_logged_out({ different: 'thing', happening: 'here' });
var rabbit = require('wascally');
rabbit.handle('publisher.message', function(msg) {
console.log('Received:', JSON.stringify(msg.body));
msg.ack();
});
require('config/rabbitmq')(rabbit, 'example-events');
console.log('worker started');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment