@pixellot/pxlt-rabbit-handler
Version:
A generic class that handles RabbitMQ connection, consume and produce functionality.
24 lines (18 loc) • 904 B
JavaScript
const amqp = require('amqplib');
const config = require('config');
const { RabbitMQAPI } = require('./utilities/rabbitmq-http-api');
module.exports = async function setup() {
const url = new URL('amqp://localhost');
url.host = config.get('rmq.host');
url.port = config.get('rmq.amqpPort');
url.username = config.get('rmq.username');
url.password = config.get('rmq.password');
url.pathname = config.get('rmq.vhost');
const connection = await amqp.connect(url.toString())
.catch((err) => { throw new Error(`Failed to create RMQ connection: ${err.message}`); });
const channel = await connection.createConfirmChannel()
.catch((err) => { throw new Error(`Failed to create RMQ channel on connection: ${err.message}`); });
global.__RMQ_CONNECTION__ = connection;
global.__RMQ_CHANNEL__ = channel;
global.__RMQ_HTTP_API__ = new RabbitMQAPI();
};