Skip to content

Instantly share code, notes, and snippets.

@prabirshrestha
Created March 10, 2014 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prabirshrestha/9465820 to your computer and use it in GitHub Desktop.
Save prabirshrestha/9465820 to your computer and use it in GitHub Desktop.
sample bus wrapper
/*
* usage:
* var Bus = require('../utils/bus');
* var bus = new Bus({ url: 'amqp://guest:guest@localhost:5672' });
* bus.connect(); // or bus.connectWithRetry();
*
* bus.createQueueIfNotExists('queueName', function (err) {
* if (err) return console.log('error creating queue', err);
* console.log('queue created successfully');
* });
*
* bus.send('queueName', { data: 'something' }, function (err) {
* if (err) return console.log(err);
* console.log('message sent');
* });
*
* bus.createTopicIfNotExists('topicName', function (err) {
* if (err) return console.log('error creating topic', err);
* console.log('topic created successfully');
* });
*
* // topic must be created first before creating topic queue
* bus.createTopicQueueIfNotExists('topicName', 'queueName', 'routingKey', function (err) {
* if (err) return console.log('failed to create topic queue', err);
* console.log('topic queue created successfully');
* });
*
* bus.publish('topicName', 'routingKey', { data: 'something' }, function (err) {
* if (err) return console.log(err);
* console.log('message published');
* });
*
* // handles both queue and topic queues
* bus.on('queueName', function (msg, next) {
* console.log(msg);
* next(); // call next to ack or next(new Error()) to not ack
* }, function (err) {
* if (err) return console.log('failed to registered handler');
* console.log('handler registered successfully');
* });
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment