Rabbitmq on Node.js with ampqlib not broadcasting the messages

Asked

Viewed 12 times

0

This script should be a command-line chat type only for rabbitmq study.

Executions of the program would be opened in parallel. Each execution creates a Sumer from the same queue and all text sent by a program execution should be sent to all consummers via an exchange of type fanout.

It turns out that although I set up exchange as fanout it is not broadcasting the messages. It is behaving like a load Sound sending the message to each Sumer at a time.

const amqplib = require('amqplib');
const readline = require("readline");

class Chat {

    async init() {
        await this.configureChannel();
        await this.configureConsumer();
        await this.configureCommandLine();
    }

    async configureChannel() {
        const conn = await amqplib.connect('amqp://guest:guest@localhost:5672');
        const ch = await conn.createChannel();
        await ch.assertExchange("chat", "fanout", {});
        const { queue } = ch.assertQueue('messages');
        await ch.bindQueue(queue, 'chat', '');
        this.ch = ch;
    }

    async configureConsumer() {
        await this.ch.consume("messages", logMessage);
        function logMessage(msg) {
            if (msg.content)
                console.log("\n[*] Recieved message: '%s'", msg.content.toString())
        }
    }

    async configureCommandLine() {
        const commandLine = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });
        this.commandLine = commandLine;
    }

    async run() {
        const prompt = () => {
            this.commandLine.question("Message: ", async (mensagem) => {
                debugger;
                if (mensagem === "sair") {
                    return this.commandLine.close();
                }
                await this.ch.publish("chat", 'messages', Buffer.from(mensagem), {});
                prompt();
            });
        }
        await this.init();
        console.log("\nChat\n");
        prompt();
    }

}

const chat = new Chat();
chat.run();

1 answer

0


Based on the answer given in Stackoverflow: https://stackoverflow.com/questions/68324760/rabbitmq-innode-js-with-amqplib-not-broadcasting-fanout-messages/68329407#68329407

Assuming you run multiple instances of this script as is, the problem here is that you are using a queue for all consumers. A fanout exchange sends each received message to all queues that are linked to the exchange. But each queue (if it has multiple consumers) will work in the caster style (round-Robin) - assuming there is some limit on the Effect count. To get fanout behavior, you need to run each instance of the script with a different queue.

The solution would be in the changes of these lines:

...
async configureChannel() {
    const conn = await amqplib.connect('amqp://guest:guest@localhost:5672');
    const ch = await conn.createChannel();
    await ch.assertExchange("chat", "fanout", {});
    this.ch = ch;
}

async configureConsumer() {
    const { ch } = this;
    const { queue } = await ch.assertQueue('', { exclusive: true });
    await ch.bindQueue(queue, 'chat', '');
    ch.consume(queue, logMessage);
    function logMessage(msg) {
        if (msg.content)
            console.log("\n[*] Recieved message: '%s'", msg.content.toString())
    }
}
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.