amqplib-wrapper
v1.0.6
Published
Amqplib wrapper for RabbitMQ
Downloads
6
Readme
Amqplib-wrapper
This is a simple wrapper module to abstract complexity from amqplib.
Usage
const amqp = require('amqplib');
const AmqpWrapper = require('./index');
let config = {
protocol: "amqp",
hostname: "localhost",
port: 5672,
username: "guest",
password: "guest"
};
let wrapper = new AmqpWrapper(amqp, config);
// Publish
wrapper.publish(exchange, routingKey, message, options)
.then(() => console.log('sent'));
// Consume with ack
wrapper.consume(queue, prefetch, true, (msg, ack, nack) => {
let content = msg.content.toString();
// DO STUFF
if (condition) {
ack();
} else {
nack();
}
});
// Consume without ack
wrapper.consume(queue, prefetch, false, msg => {
let content = msg.content.toString();
// DO STUFF
});
// Stop consuming from specific queue
wrapper.stopConsuming(queue).then(() => console.log('stopped'));
// Stop consuming from all queues
wrapper.stopConsuming().then(() => console.log('stopped'));
// Purge all messages from queue
wrapper.purge(queue).then(() => console.log('purged'));
// Get number of messages in queue
wrapper.getQueueLength(queue).then(length => console.log(length));
// Check connection
let connected = wrapper.isConnected();
// Close the connection gracefully
wrapper.close().then(() => console.log('closed'));