Search code examples
node.jsrabbitmqamqp

AMQP RabbitMQ get missed published messages


When using send/listen which is 1:1 on rabbitmq, the service keeps the messages that haven't been listened to or acknowledged by the listener so that when a listener is up, it receives the backlog and it is cleared. How can or what configuration is required to make this also work for publish/subscribe which is 1:many (fanout-like)?

I am using amqplib for nodejs


Solution

    1. Rabbitmq queue should be durable

    2. This makes sure the queue is declared before attempting to consume from it

    durable: true
    });
    
    **sample code**
    
    var amqp = require('amqplib/callback_api');
    
    amqp.connect('amqp://localhost', function(error0, connection) {
      if (error0) {
        throw error0;
      }
     connection.createChannel(function(error1, channel) {
        if (error1) {
          throw error1;
        }
       var queue = 'task_queue';
    
       var msg = process.argv.slice(2).join(' ') || "Hello World!";
    
        channel.assertQueue(queue, {
          durable: true
        });
        channel.sendToQueue(queue, Buffer.from(msg), {
          persistent: true
        });
       console.log(" [x] Sent '%s'", msg);
      });
      setTimeout(function() {
        connection.close();
        process.exit(0)
      }, 500);
    });
    
    
    ----------