Consuming messages one by one in NodeJS

762 views
Skip to first unread message

András Szabó

unread,
Dec 5, 2017, 6:58:20 PM12/5/17
to rabbitmq-users

By default, the consume() function consumes all the messages that are in the queue, but I need pop only one message from the queue, and with the next consume() call pop the second message from the queue, etc. I have this code:


rabbit.js (in this file I have implemented the sending to and receiving from the queue):


var amqp = require('amqplib/callback_api');
var ch;

amqp
.connect('amqp://localhost', function(err, conn) {
  conn
.createChannel(function(err, channel) {    
    ch
= channel;
 
});
});

module.exports.queueSend = function(msg) {
   
var q = 'hello';

    ch
.assertQueue(q, {durable: false});
    ch
.sendToQueue(q, new Buffer(msg));
    console
.log(" [x] Sent %s", msg);
}

module.exports.queueReceive = function() {
   
var q = 'hello';

    ch
.assertQueue(q, {durable: false});
    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());
   
}, {noAck: false});
}


rabbit_test.js (in this file I have tested the functionality of the previous code):


var rabbit = require("./rabbit");

setTimeout
(function() {
    rabbit
.queueSend("01C");
    rabbit
.queueSend("02C");
    rabbit
.queueReceive();
}, 2000);


When I call the queueReceive() function, it prints out the "01C" and "02C" messages too, not only the "01C". I need print out only the "01C" and with a next queueReceive() call the "02C".


P.S.: I tried this code too in rabbit.js for function queueReceive():


module.exports.queueReceive = function() {
   
var q = 'hello';

    ch
.prefetch(1);
    ch
.assertQueue(q, {durable: false});
    console
.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch
.consume(q, function(msg) {
      ch
.ack(msg);
      console
.log(" [x] Received %s", msg.content.toString());
   
}, {noAck: false});
}


I added the ch.ack(msg) and ch.prefetch(1) parts, but it doesn't works neither.


How can i get this working? Thank you in advance!

Luke Bakken

unread,
Dec 5, 2017, 8:47:42 PM12/5/17
to rabbitmq-users
Hello András -

I don't know the amqplib API off the top of my head, but I bet there's a channel.get() method you can use instead of channel.consume()

Luke

On Tuesday, December 5, 2017 at 3:58:20 PM UTC-8, András Szabó wrote:

By default, the consume() function consumes all the messages that are in the queue, but I need pop only one message from the queue, and with the next consume() call pop the second message from the queue, etc. I have this code:


András Szabó

unread,
Dec 5, 2017, 9:28:15 PM12/5/17
to rabbitmq-users
Thanks Luke! It works for me. :)
Message has been deleted

András Szabó

unread,
Dec 6, 2017, 9:23:04 PM12/6/17
to rabbitmq-users
An another question: is it possible to get given amount of messages from queue? The get() method gets one message, the consume() gets the all messages, but is there a method for getting a given amount (e.g. three) of messages? Thanks!

Luke Bakken

unread,
Dec 7, 2017, 10:29:07 AM12/7/17
to rabbitmq-users
Hi András -

Please do not follow up a question with a new question in the same discussion. The reason is that people will probably not notice your new question.

Michael Klishin

unread,
Dec 8, 2017, 12:50:32 PM12/8/17
to rabbitm...@googlegroups.com
The "how do I know how many messages are in the queue" question has been discussed numerous times all over the Internet
in the last few years. Please do some research.

--
You received this message because you are subscribed to the Google Groups "rabbitmq-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-users+unsubscribe@googlegroups.com.
To post to this group, send email to rabbitmq-users@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
MK

Staff Software Engineer, Pivotal/RabbitMQ
Reply all
Reply to author
Forward
0 new messages