Created
January 28, 2016 16:31
node/rabbit example
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
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: ['#'] | |
} | |
] | |
}); | |
}; |
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 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 | |
}); | |
} |
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 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' }); |
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 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