title: How to stop consuming message from selective queue - RabbitMQ - Javascript/node
body: <p>I am building a REST-amqp sample in which I get messages from a given queue in rabbitmq and I send the messages back to the client via REST.</p>
  
  <p>I have implemented the code following rabbitmq tutorial for node.js </p>
  
  <pre><code>var amqp = require('amqplib/callback_api');
  amqp.connect('amqp://192.168.225.203:5672', function (err, conn) {
  conn.createChannel(function (err, ch) {
      var q = 'aQueue';
      ch.assertQueue(q, { durable: false });
      var messages = [];
      console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
      ch.consume(q, function (msg) {
          console.log(" [x] Received %s", msg.content.toString());
          messages.push(msg.content.toString());
          messages.forEach(function(element) {
              console.log(element);
          });
      }, { noAck: true });
  });
  </code></pre>
  
  <p>});</p>
  
  <p>I can consolidate all messages in an array (messages) but this function runs till the application is stopped, which is not a good thing as the REST client will wait forever....
  I would like to stop the function and move on with the program when the queue is empty or after a period of time (2secs) if the first option is not possible.
  I found the same problem solved in Java but not in JS.</p>
  
  <p>Any hint is much appreciated.</p>
  
  <p>Cheers, Giovanni</p>