RabbitMQ router connect with node getting Error: connect ECONNREFUSED 127.0.0.1:5672

974 Views Asked by At
const amqp = require("amqplib");
const config = require("./config/config");

class Producer {
    channel;

    async createChannel() {
        const connection = await amqp.connect(config.rabbitMQ.url);
        this.channel = await connection.createChannel();
    }

    async publishMessage(routingKey, message) {
        if (!this.channel) {
            await this.createChannel();
        }

        const exchangeName = config.rabbitMQ.exchangeName;
        await this.channel.assertExchange(exchangeName, "direct");

        const logDetails = {
            logType: routingKey,
            message: message,
            dateTime: new Date(),
        };
        await this.channel.publish(
            exchangeName,
            routingKey,
            Buffer.from(JSON.stringify(logDetails))
        );

        console.log(
            `The new ${routingKey} log is sent to exchange ${exchangeName}`
        );
    }
}

module.exports = Producer;

Error

node:internal/process/promises:279
        triggerUncaughtException(err, true /* fromPromise */);
        ^

Error: connect ECONNREFUSED 127.0.0.1:5672 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16) { errno: -111, code: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 5672 }

0

There are 0 best solutions below