How to consume just one message from rabbit mq on nodejs

node.js, rabbitmq

Solution

When you create the model you need to set the QOS on it. Here is how we would do it in C#:

    var _model = rabbitConnection.CreateModel();
    // Configure the Quality of service for the model. Below is how what each setting means.
    // BasicQos(0="Dont send me a new message untill I’ve finshed",  _fetchSize = "Send me N messages at a time", false ="Apply to this Model only")
    _model.BasicQos(0, _fetchSize, false);
    var consumerTag = _model.BasicConsume(rabbitQueue.QueueName, false, _consumerName, queueingConsumer);

Problem

Im using amqp.node library to integrate rabbitmq into my system. But in consumer i want to process just one message at the time, then acknowledge the message then consume the next message from the queue. The current code is: ``` // Consumer open.then(function(conn) { var ok = conn.createChannel(); ok = ok.then(function(ch) { ch.assertQueue(q); ch.consume(q, function(msg) { if (msg !== null) { othermodule.processMessage(msg, function(error, response){ console.log(msg.content.toString()); ch.ack(msg); }); } }); }); return ok; }).then(null, console.warn); ``` The ch.consume will process all messages in the channel at one time and the function of the module call it here othermodule will not be executed in the same time line. I want to wait for the othermodule function to finish before consume the next message in the queue.

Original source