rabbitmqconfirmpublisher

RabbitMQ ConfirmChannel publish method


I am testing RabbitMQ with this "Hello World" code in JS

publisher.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost:5672', function(error0, connection) {
  if (error0) {
    throw error0;
  }
  connection.createConfirmChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }
    var msg = 'Hello world';
    channel.assertExchange('exchange2', 'fanout', {
        durable: false
      });
    channel.publish('exchange2','', Buffer.from(msg), {},publishCallback);
    channel.waitForConfirms(function(err) {if (err) console.log(err); else console.log('success');})
    console.log(" [x] Sent %s", msg);
  });
});

function publishCallback(err, ok) {
    if (err !== null) {
      // Handle the error if there was a problem with publishing
      console.error('Error:', err);
    } else {
      // The message was successfully published
      console.log('Message published successfully:', ok);
    }
  }

receive.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost:5672', function(error0, connection) {
  if (error0) {
    throw error0;
  }
  connection.createChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }
    var queue = 'hello';

    channel.assertExchange('exchange2', 'fanout', {
        durable: false
      });
  
      channel.assertQueue('', {
        exclusive: true
      }, function(error2, q) {
        if (error2) {
          throw error2;
        }
        console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
        channel.bindQueue(q.queue, 'exchange2', '');
  
        channel.consume(q.queue, function(msg) {
          if(msg.content) {
              console.log(" [x] %s", msg.content.toString());
            //   channel.nack(msg, false, false)
            }
        }, {
          noAck: false
        });
      });
    });
});

What I want is to have a publisher confirm about the acknownledgement of the message. But my problem is that the 'channel.waitForConfirms' always print "success" even there still is unacked message in the queue.

Has anybody an idea of what is going on ?

I tried to use a callback in the publish method but the "err" argument is null and the "ok" argument is undefined no matter the message is acked or not.


Solution

  • Publisher Confirms : messages the client publishes are confirmed asynchronously by the broker, meaning they have been taken care of on the server side.

    Unacked message : consumer fails to acknowledge messages

    Publisher Confirms is about publisher - server (rabbitMQ ). Unacked about server - consumer

    So, in this case, server confirms messages is stored on queue (success), but not yet acked by consumer