title: How to properly check if RabbitMQ channel has been opened after reconnection in Node.js?
body: <p>I'm using <code>amqplib</code> which I'm trying to implement a reconnecting mechanism. However, after the connection is reestablished it looks like my channel is still closed. How do I fix this issue? This is my code. </p>
  
  <pre><code>var pubQueue = [];
  module.exports = {
    connect: function (callback) {
      var self = this;
      amqp.connect(config.queue.url, function(err, connection) {
        if (err) {
          console.error("[AMQP]", err.message);
          return setTimeout(module.exports.connect, 2000);
        }
  
        connection.on("error", function(err) {
          if (err.message !== "Connection closing") {
            console.error("[AMQP] conn error", err.message);
          }
        });
  
        connection.on("close", function() {
          console.error("[AMQP] reconnecting");
          return setTimeout(module.exports.connect, 2000);
        });
  
        connection.createChannel(function(err, ch) {
          console.log('connection is reestablished');
          self.channel = ch;
          return callback ? callback() : false;
        });
      });
    },
  
    publish: function (message, callback) {
      var self = this;
      var key = this.generateKey(message);
      var m = new Buffer(JSON.stringify(message));
  
      try {
        self.channel.assertExchange(config.queue.exchange, 'topic', {durable: false, nowait: true});
        self.channel.publish(config.queue.exchange, key, m, {nowait: true});
        return callback ? callback() : true;
      } catch(err) {
        pubQueue.push({key: key, m: m});
        console.log(err);
      }
    }
  } 
  </code></pre>
  
  <p><code>connect()</code> will be called once the express application is started. But <code>publish</code> will be call on every request. That's why I have <code>pubQueue</code> to store the missing messages. I haven't implemented the function to resend the messaged in the queue but I got this error first which I can't seem to wrap my head around it. </p>
  
  <pre><code>connection is reestablished
  { [IllegalOperationError: Channel closed]
    message: 'Channel closed',
    stack: 'IllegalOperationError: Channel closed\n    at Channel.}
  </code></pre>