Search code examples
node.jsrabbitmqamqp

How to get number of messages in queue with node-amqp


I'm using node-amqp as a queueing system in a node application. I'd like to be able to monitor the state of the queue to figure out if we have enough workers runnning, i.e. if the queue size is increasing we know we are starting to fall behind.

I know that from the command line you can use something like;

rabbitmqctl list_queues

Which gives me the exact information I need, but I was wondering if there is anyway to do this from node-amqp itself?

Thanks in advance.

EDIT

In the end, I just used the command line tool rabbitmqctl to get the information I need, its not a great solution but here is what I did;

var Logger = require('arsenic-logger');

getQueueMeta(function(info){
    Logger.info(info);
});

/**
* Returns a sparse array with the queue names as the indices
* and the number of messages as the value, e.g.;
*
* info = [ my-queue: 9, my-other-queue: 1 ]
* 
* @param callback
*/
function getQueueMeta(callback){

    var sys = require('sys')
    var exec = require('child_process').exec;

    exec("/usr/local/sbin/rabbitmqctl list_queues", function(error, stdout, stderr) {

        var info = [];

        if (!error){

            var lines = stdout.split(/\n/);

            if (lines.length > 1){

                for (var i=1; i<lines.length-2; i++){
                    var temp = lines[i].split(/\s/);
                    info[temp[0].trim()] = parseInt(temp[1]);
                }

            }

        }

        callback(info);

    });

}

Solution

  • I may be a bit late for the party, but in case anyone in the future stumbles across this problem... In current version of node-amqp (0.2.4) you can check both message count and number of currently subscribed consumers when declaring a queue.

    var connection = require("amqp").createConnection();
    connection.exchange("exampleExchange", {/*...*/}, function(exchange) {
      connection.queue("exampleQueue", {/*...*/}, function(queue, messageCount, consumerCount){
        console.log("Message count", messageCount);
        console.log("Consumer count", consumerCount);
      });
    });