Wait until node-amqp has sent a message?

1.4k views Asked by At

If I use the following code to send a message to RabbitMQ:

var amqp = require('amqp');
var connection = amqp.createConnection();
connection.on('ready', function() {
  connection.publish('foo', 'Hello');
  process.exit();
});

...then the message is never sent, and the process just quits. If I remove process.exit(), then the message is sent, but the process never quits.

How do I get node-amqp to tell me when it has sent a message?

1

There are 1 answers

0
Pradeep Mahdevu On BEST ANSWER

Use a callback in the publish function. End the connection in the callback and exit.

connection.publish('foo', 'hello', function(){
   connection.end();
   process.exit();
});