Saturday 18 March 2017

AMQP (Node.js) for RabbitMQ close connection too early.

I have the following code, and it's giving me the following error.

TypeError: Cannot read property 'assertQueue' of undefined
    at /var/www/myapp/dashboard/routes.js:195:39
    at /var/www/myapp/node_modules/amqplib/lib/callback_model.js:46:16
    at /var/www/myapp/node_modules/amqplib/lib/callback_model.js:61:10
    at /var/www/myapp/node_modules/amqplib/lib/callback_model.js:74:5

If I comment out conn.close(), and the code works fine, and I think the code is trying to close the conn too early, before the execution of ch.assertQueue. What is the best way to fix this problem?

 amqp.connect('amqp://localhost', function(err, conn) {
                                  conn.createChannel(function(err, ch) {
                                    var q = 'blast_queue';
                                    var msg = blast.id;

                                    ch.assertQueue(q, {durable: true});
                                    ch.sendToQueue(q, new Buffer(msg), {persistent: true});
                                    console.log(" [x] Sent '%s'", msg);
                                  });
                                  conn.close();
  });



via user1187968

No comments:

Post a Comment