0

I'm trying to pull a single message off of rabbitmq, process it, and acknowledge that single message. I don't want it to continue after acknowledgement pulling messages off the queue. If I don't do ch.close() in the code below, it continues to pull messages after acknowledging the previous message.

Using a docker RabbitMQ instance I've set up this code (which works) but I'm curious if open/closing channels quickly are the best way to do this:

amqp.connect('amqp://guest:guest@localhost', (err, conn) => {
 conn.createChannel((err, ch) => {
  if (err) {
   console.error(err);
  } else {
   let q = 'my queue';
   ch.prefetch(1);
   ch.consume(q, data => {
    console.log(data.content.toString());
    ch.ack(data);
    ch.close();
    returnData = data;
   });
  }
 });
});
Dipak
  • 2,248
  • 4
  • 22
  • 55
Bryce
  • 664
  • 6
  • 17
  • wonder if this is a better question for codereview.stackexchange.com – Jarede Feb 15 '18 at 15:36
  • Reposted there as well, curious though if anyone has solved this problem. It "works" but it seems like there has to be a better way than closing a channel each time you pull a message (perhaps it is though). – Bryce Feb 15 '18 at 18:40

1 Answers1

2

You can pull messages one at a time, using channel.get (see http://www.squaremobius.net/amqp.node/channel_api.html#channel_get), I find this can be useful, rather than using consume (even with prefetch). You can use a long lived channel as well for this purpose.

var amqpChannel = null;

amqp.connect('amqp://guest:guest@localhost', (err, conn) => {
    conn.createChannel((err, ch) => {
        if (err) {
            console.error(err);
        } else {
            amqpChannel = ch;
        }
    });
});

var readMessageFromQueue = function() {
    if (amqpChannel) {
        amqpChannel.get(q, data => {
            // data will be set to false if no messages are available on the queue.
            if (data) {
                console.log(data.content.toString());
                amqpChannel.ack(data);
            }
        });
    }
}

// Whatever interval you like..
setInterval(readMessageFromQueue, 1000);
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40