I am using amqplib to transfer messages in my node.js server. I saw an example from RabbitMQ official website:
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(err, conn) {
conn.createChannel(function(err, ch) {
var q = 'hello';
var msg = 'Hello World!';
ch.assertQueue(q, {durable: false});
// Note: on Node 6 Buffer.from(msg) should be used
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
In this case, the connection is closed in an timeout function. I don't think this is a sustainable way to do it. However, ch.sendToQueue doesn't have a callback function allowing me to close connection after message is sent. What's a good point to close connection?