UNPKG

@srsl/tools

Version:

JavaScript tools for common interfaces

32 lines (25 loc) 804 B
const ampqlib = require('amqplib') class Rmq { constructor({ url, ...options }) { this.ampqlib = ampqlib this.REPLY_QUEUE = 'amq.rabbitmq.reply-to' || options.replyQueue this.url = url this.options = options Object.entries(options).forEach(([k, v]) => {this[k] = v}) } async open() { return ampqlib.connect(this.url || process.env.AMPQ_URL || process.env.RMQ_URL || 'amqp://127.0.0.1:5672/') } async sendToQueue(queue, pattern, data, options = {}) { const connection = await this.open() const channel = await connection.createConfirmChannel() const result = await channel.sendToQueue(queue, new Buffer.from(JSON.stringify({ pattern, data, }))) } async emit(...args) { return this.sendToQueue(...args) } } module.exports = Rmq