Skip to content Skip to sidebar Skip to footer

Javascript Rabbitmq -> Pull Single Message

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 que

Solution 1:

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);

Post a Comment for "Javascript Rabbitmq -> Pull Single Message"