21 lines
688 B
TypeScript
21 lines
688 B
TypeScript
import * as amqp from 'amqplib';
|
|
import { logger, RABBITMQ_URI } from '../app';
|
|
|
|
export class RabbitMQ {
|
|
connection: amqp.Connection | null = null;
|
|
channel: amqp.Channel | null = null;
|
|
|
|
async init() {
|
|
this.connection = await amqp.connect(RABBITMQ_URI);
|
|
this.channel = await this.connection.createChannel();
|
|
|
|
this.channel.consume('Tracker', (msg) => {
|
|
logger.debug("Received from broker: " + msg?.content.toString());
|
|
}, { noAck: false });
|
|
}
|
|
|
|
async publish(queueName = 'Tracker', data: any) {
|
|
if (this.connection == undefined) await this.init()
|
|
this.channel?.sendToQueue(queueName, Buffer.from(data));
|
|
}
|
|
} |