2

(I am using node-amqp and rabbitmq server.)

I am trying to guess why I have a close event if something goes wrong. For example, If I try to open a a connection to a queue (with bad parameters) I receive an error event. That it is perfect ok.

But, after any error I will receive also a close connection (in that case, maybe because close the failed socket to the queue). And after that, auto-reconnect and I receive the (initial) ready event.

The problem:

connection.on('ready', function() {
 do_a_lot_of_things

}).on(error, function(error){
 solve_the_problem
});

if something goes wrong, I receive the error, but then "ready" event and it will re do_a_lot_of_things. Is my approach wrong?

best regards

pinepain
  • 12,453
  • 3
  • 60
  • 65
jgato
  • 181
  • 1
  • 6
  • I think the reason is that when you try to create a queue (wrong configuration) into rabbitmq this emmit and error and after that it closes the connection. After that, node-amqp make the reconnect (if configured in that way). If connection success ready event is raised again and do_a_lot_of_things is done again. – jgato Jan 13 '14 at 12:21

1 Answers1

2

You can use connection.once('ready', function () { … }) (see the documentation), which will execute the handler only on the first event.

Paul Mougel
  • 16,728
  • 6
  • 57
  • 64
  • This seems OK to solve my current problem. But, Why am I receiving this sequence of close/ready? If something really important happens, for example: the rabbit server goes down, then I would like to know when it is up again receiving the "ready" event. Thus, I could react to it. – jgato Jan 13 '14 at 06:11
  • (I'm not familiar with the node-ampq module). You can also add a second listener with `.on('ready')` to react on those events. – Paul Mougel Jan 13 '14 at 10:03