RabbitMQ with Node.js

74 views
Skip to first unread message

Andrew Collins

unread,
Dec 10, 2014, 3:42:21 PM12/10/14
to rabbitm...@googlegroups.com
I am looking to use RabbitMQ with Node.JS to pass messages from a web page to MongoDB, I am new to Node and having issues with amp or amqplib.

Which one should be used?

I have successfully used the rabbitMQ tutorials for both amp and amqplib, but the amp didn't work as expected with the rabbit management screen, in that no messages ever appeared on the queued messages graph, even if the subscriber wasn't running.

The syntax for amqplib seems more complicated with the returns etc, whereas the amqp used the .on events.

Should i worry about posting to an exchange or just the queue?

Thanks

Michael Klishin

unread,
Dec 10, 2014, 3:47:08 PM12/10/14
to Andrew Collins, rabbitm...@googlegroups.com
 On 10 December 2014 at 23:42:23, Andrew Collins (andymc...@gmail.com) wrote:
> Which one should be used?

amqp.node. Avoid node-amqp as it has known bugs and is not really maintained.

> I have successfully used the rabbitMQ tutorials for both amp
> and amqplib, but the amp didn't work as expected with the rabbit
> management screen, in that no messages ever appeared on the queued
> messages graph, even if the subscriber wasn't running.

This means they were never routed. It doesn't matter if you have consumers online or not,
if messages are routed, it will be seen on the charts [of target queues].

>
> The syntax for amqplib seems more complicated with the returns
> etc, whereas the amqp used the .on events.

amqp.node has a callback API as well as futures (promises):
https://github.com/squaremo/amqp.node#callback-api-example
https://github.com/squaremo/amqp.node#promise-api-example

> Should i worry about posting to an exchange or just the queue?

See http://www.rabbitmq.com/tutorials/amqp-concepts.html. You never publish to queues,
the sendToQueue function which I believe amqp.node has uses default exchange. There is nothing
wrong with using that function but eventually you'll have to get familiar with other exchanges [exchange types].
--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Andrew Collins

unread,
Dec 11, 2014, 7:13:33 AM12/11/14
to rabbitm...@googlegroups.com, andymc...@gmail.com
Thanks for you help

I have started looking at the with promise API, and message are showing up on the Message rate graph, but only showing on the Queued messages graph when the consumer isnt running.

When both Published and consumer are running the message rate for acknowledged (red line) goes up, but nothing on Queued messages

When only the publisher is running the message rate for publish goes up (Yellow line) and the ready & total lines go up on the Queued Messages.
As soon as consumer starts the messages appear and lines return to zero.

is this normal?
Is it because they are getting acknowledged when consumed?

Thanks Again



Publisher code
var q = 'tasks';
var e = 'exchange';

var open = require('amqplib').connect('amqp://192.168.1.14');

// Publisher

open
.then(function(conn) {
 
var ok = conn.createChannel();
  ok
= ok.then(function(ch) {
    ch
.assertQueue(q,{durable: true, autoDelete: false, confirm: true});
    ch
.assertExchange(e)
    ch
.bindQueue(q,e)

     
for(var i=0;i < 10000;i++){
         ch
.sendToQueue(q, new Buffer('Test Messeage ' + i));
         console
.log('Message sent :' )
       
}
 
});
 
return ok;
}).then(null, console.warn);


Subscriber Code
var q = 'tasks';
var e = 'exchange';
var open = require('amqplib').connect('amqp://192.168.1.14');

// Consumer
open
.then(function(conn) {
 
var ok = conn.createChannel();
  ok
= ok.then(function(ch) {
    ch
.assertQueue(q,{durable: true, autoDelete: false, confirm: true});
    ch
.assertExchange(e)
    ch
.bindQueue(q,e)
    ch
.consume(q, function(msg) {

     
if (msg !== null) {
        console
.log(msg.content.toString());
        ch
.ack(msg);
     
}
   
});
 
});
 
return ok;
}).then(null, console.warn);

Michael Klishin

unread,
Dec 11, 2014, 7:19:26 AM12/11/14
to Andrew Collins, rabbitm...@googlegroups.com
On 11 December 2014 at 15:13:35, Andrew Collins (andymc...@gmail.com) wrote:
> I have started looking at the with promise API, and message are
> showing up on the Message rate graph, but only showing on the Queued
> messages graph when the consumer isnt running.
>
> When both Published and consumer are running the message rate
> for acknowledged (red line) goes up, but nothing on Queued messages
>
> When only the publisher is running the message rate for publish
> goes up (Yellow line) and the ready & total lines go up on the Queued
> Messages.
> As soon as consumer starts the messages appear and lines return
> to zero.
>
> is this normal?

Andrew,

If you have both publisher and consumer running, and consumer can keep up, there will
be no messages enqueued (waiting to be consumed) most of or all the time.

If you shut down your consumer, they will pile up in a queue (or queues), which is reflected
on the chart per your own words.

All of this makes sense if you ask me.

> Is it because they are getting acknowledged when consumed?

I highly recommend going through the tutorials before you try to evaluate
client libraries and assume they may be issues with them:

http://www.rabbitmq.com/getstarted.html
https://github.com/squaremo/amqp.node/tree/master/examples/tutorials

Andrew Collins

unread,
Dec 11, 2014, 3:13:04 PM12/11/14
to rabbitm...@googlegroups.com
Thanks for help.

Im not saying there are issues with any of the code, just asking if the results i am seeing is what should be happening.
Reply all
Reply to author
Forward
0 new messages