import { connect, Connection, Channel } from 'amqplib'; export class RabbitMQ { connection: Connection; channel: Channel; constructor() { this.connection = null; this.channel = null; } // Connect to the RabbitMQ server, create a channel, and declare an exchange public async connect(host: string) { this.connection = await connect(host, { // Retry to connect every 10 seconds for 2 minutes reconnectTimeInSeconds: 10, reconnectLimit: 12, }); this.channel = await this.connection.createChannel(); } // Publish a message to the exchange public async publish(exchange: string, routingKey: string, message: string) { const MessageOptions = { contentType: 'application/octet-stream', }; this.channel.publish(exchange, routingKey, Buffer.from(message), MessageOptions); } async disconnect() { try { await this.channel.close(); await this.connection.close(); } catch (error) { console.log(error); } } }