RabbitMQ function not working

Hi guys, I need some help

I would like to create a listener connected to rabbitMQ

I imported the lib via
- NODE_FUNCTION_ALLOW_EXTERNAL=axios,amqplib

both libraries work but in my code below I can’t read the messages from the queue

const amqp = require('amqplib');

const queue = "getScrap";

async function name() {
    const results = [];

    try {
        const connection = await amqp.connect({
            hostname: '127.0.0.1',
            port: 5672,
            username: 'zap',
            password: 'hello'
        });

        const channel = await connection.createChannel();

        await channel.assertQueue(queue, { durable: false });

        console.log("Aguardando mensagens...");

        await channel.consume(queue, async (message) => {
            if (message) {
                const content = JSON.parse(message.content.toString());
                console.log("Mensagem recebida:", content);
                
                results.push({ json: content });
                
                channel.ack(message);
            }
        }, { noAck: false });


        await channel.close();
        await connection.close();

    } catch (error) {
        console.error("Erro ao consumir mensagens:", error);
    }

    return results;
}

return name();

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @AlexCampo

Why are you trying to do it with an external library if there is a RabbitMQ node there?

Thanks for the answer

the node that exists does not do the work of listening and consuming the queue, it just picks up messages from the moment it is active

my idea with this lib is to transform this code node into a listener

Hi @AlexCampo

Welcome to the community.

Not sure what you are refering to when you say that it doesnt consume the queue.
I use RabbitMQ a lot in workflows and it working perfectly with the nodes provided.
There is 2 nodes, one normal and one trigger. You of course would need the trigger node to start consuming the queue. :slight_smile:

Hi @BramKn

the node consumes the queue but only from the moment it is 100% active, if the node is unavailable or is unable to process all messages, those that are stopped in the queue will not be processed by the node as it is reactive

@Jon He said in another topic that this would be a future feature, while we don’t think about doing something using code

Didn’t get the point. The trigger node starts to consume the queue as early as you will activate the workflow. The node can’t be unavailable, only the broker could, but in this way, you won’t get any messages from it either. The node is able to process everything you would ask it to process. It depends on how you are going to design the error handling (flush the message on error / keep it unless the workflow ends and so on).

The topic you have mention is about the delaying the message processing

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.