adonis-rabbitmq-provider
v1.4.1
Published
Provider for handle RabbitMQ messages designed for AdonisJs 4.1
Downloads
15
Maintainers
Readme
adonis-rabbitmq-provider
This package is intended to be use as a provider for AdonisJS 4.1 in order to connect, publish and consume messages on RabbitMQ service.
INSTALATION
npm install adonis-rabbitmq-provider
CONFIGURATION
- Create a configuration file config/queue.js
'use strict'
module.exports = {
url: 'amqp://<username>:<password>@<hostname>:<port>',
prefix: 'foo'
}
The key "prefix" is optional. If defined it will be used to prepend the queue name on publishing/consuming operations.
- Edit the providers list inside the file start/app.js
const providers = [
'@adonisjs/framework/providers/AppProvider',
'@adonisjs/auth/providers/AuthProvider',
[...]
'adonis-rabbitmq-provider/providers/RabbitMQProvider' // Add this line to the providers array
];
USAGE
const RabbitMQProvider = use('RabbitMQProvider');
// Publishing messages
RabbitMQProvider.send(queueName, payload[, options]);
Example:
var jobId = await RabbitMQProvider.send('myQueueName', {foo: 'bar'});
Getting one message syncronously
RabbitMQProvider.get(queueName);
Example:
// Retrieving a single message from the queue
const message = await RabbitMQProvider.getMessage(queue);
// Parsing the payload
let payload = JSON.parse(message.content.toString());
// Acknowledging the message
RabbitMQProvider.ack(queueName, message);
// Or rejecting the message and putting it back to the queue
RabbitMQProvider.nack(queueName, message);
TESTS
There is no tests configured yet