1

I'm trying to achieve something very simple - connect to a RabbitMQ exchange and consume messages. I'm using https://github.com/postwait/node-amqp. Here's the NodeJS code.

var rabbitMQ = amqp.createConnection({
    host: 'localhost'
});

rabbitMQ.on('ready', function() {
    console.log('Connected to RabbitMQ');
    var exchange = rabbitMQ.exchange('foobar');

    // some more logic
});

However, when I run the server, the line console.log('Connected to RabbitMQ') gets executed repeatedly forever. Of course, this behavior disappears when I remove var exchange = connection.exchange('foobar').

I'm fairly new to NodeJS, and I've tried reading up the docs and Googling around for example code, but nothing works. What am I missing?

TeknasVaruas
  • 1,480
  • 3
  • 15
  • 28
  • What is `connection`, you haven't defined a variable called that? I think you mean `var exchange = rabbitMQ.exchange('foobar');` – Matt Harrison Jan 17 '15 at 15:41
  • @MattHarrison Apologies. It was a typo. :) – TeknasVaruas Jan 17 '15 at 15:51
  • Is that all of the code? Tried it myself and it works fine. I'm thinking there's something else going on with your code. – Matt Harrison Jan 17 '15 at 15:58
  • I'm actually using socket-io + rabbitmq. Actually, when I am trying to connect to an exchange that does not exist, it works fine. But when I connect to an exchange that already exists, it repeats infinitely. – TeknasVaruas Jan 17 '15 at 16:09
  • What do you mean by "repeat infinitely"? In RabbitMQ you don't connect to Exchanges, you connect to the broker and the declare exchanges. More details here http://www.rabbitmq.com/tutorials/amqp-concepts.html – old_sound Jan 23 '15 at 10:10
  • The infinite repeat indicates an error with the connection (very annoying feature of node-amqp). Either it can't connect at all, or there is an error in your code. – Anders Bornholm Jan 26 '15 at 10:16

0 Answers0