1

I'm using a RabbitMQ queue to publish and receive messages between consumers, the main issue is that I want to receive a single message then exit. From other answers on this site I've seen that channel.get seems to be the best way to do this. However, I can't get it to work. This is the answer I've been using.

My current code:

var amqpChannel = null;
var queue = "test";

amqp.connect(cluster, (error0, connection) => {
    if (error0) throw error0;

    connection.createChannel((error1, channel) => {
        if (error1) throw error1;

        amqpChannel = channel;
    });
});

var readMessage = function() {
    if (amqpChannel)
    {
        amqpChannel.get(queue, (err, msg) => {
            if (err) console.log(err);
            
            if (msg)
            {
                console.log("Message received: " + msg.content.toString());
                amqpChannel.ack(msg);
            }
        });
    }
}

setTimeout(readMessage, 1000);

As far as I can see, it is identical to the code in the accepted answer above, however I can't seem to get it to work. What am I missing?

Edit: Extra info

Using channel.consume works for me, it gets whatever messages are in the queue. However using the channel.get method results in nothing. I have used console.log lines to ensure the channel is being created properly, but for some reason the channel.get callback is never being triggered. I know that all the connection and queue creation is all working, I just can't seem to get the channel.get method to trigger at all.

Edit 2: I found the issue

My callback function wasn't correct. Looking at the documentation here, channel.get requires an options parameter before the callback, adding that in fixed my issue. My working code is now:

var amqpChannel = null;
var queue = "test";

amqp.connect(cluster, (error0, connection) => {
    if (error0) throw error0;

    connection.createChannel((error1, channel) => {
        if (error1) throw error1;

        amqpChannel = channel;
    });
});

var readMessage = function() {
    if (amqpChannel)
    {
        amqpChannel.get(queue, {noAck: true}, (err, msg) => {
            if (err) console.log(err);
            
            if (msg)
            {
                console.log("Message received: " + msg.content.toString());
                amqpChannel.ack(msg);
            }
        });
    }
}

setTimeout(readMessage, 1000);
Brady Harper
  • 235
  • 1
  • 3
  • 17
  • Please describe what is happening, some errors, anything? – Nikita Chayka Apr 27 '21 at 11:45
  • @NikitaChayka That's the issue, nothing is happening. I have a small send script that sends a message to a queue, when I use the ```channel.consume``` method, the message is pulled down. However, when I use the above script with the ```channel.get``` method, nothing happens. I used a couple ```console.log``` lines to ensure that the channel is being created, but the ```channel.get``` callback is never being called. I'll update the question with this info as well – Brady Harper Apr 27 '21 at 11:49
  • Have you debbugged it, do you see indeed that when readMessage is executed, amqpChannel is defined? I see one difference between what you have and other post had - you are using setTimeout, which will execute only once after 1 second, while the other post had setInterval - which would execute continiously with frequency of 1 second. Hence are you sure your channel is defined exactly after 1 second, and queue is not empty after that 1 second? – Nikita Chayka Apr 27 '21 at 11:52
  • @NikitaChayka Yeah I previously had a ```console.log("here)``` line inside the if statement to ensure that ```amqpChannel``` existed and it was always printed. But that's as far as the code gets. I used ```setTimeout``` as I only want to trigger it once, but I had the same issues when using ```setInterval``` so that has made no difference. – Brady Harper Apr 27 '21 at 11:57
  • In network tab do you see any request happening when .get is executed? – Nikita Chayka Apr 27 '21 at 11:59
  • What network tab? So far I've only been using the RabbitMQ broker logs and web UI to view connections and queues Both of which show that everything connects and is sent correctly, just never received. – Brady Harper Apr 27 '21 at 12:01
  • Another one reason may be, that channel.get returning promise and you need to do await instead of using callback. so let data = await channel.get(queueName); – Nigrimmist Nov 02 '22 at 11:38

0 Answers0