title: RabbitMQ - How to check if queue is empty?
body: <p>I have a web service interface that abstracts a RabbitMQ server (don't ask me why, I know it's an unnecessary step, but I have to). That is, I poll messages from the queue through a web service call, not directly over <code>amqp</code>.</p>
  
  <p>Consuming via <code>basic.consumer</code> blocks the execution thread till there are messages in the queue. This makes the web service not return. </p>
  
  <p>Code for illustration:</p>
  
  <pre><code>    $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
      $channel = $connection-&gt;channel();
  
      $channel-&gt;queue_declare(QUEUE_NAME, false, true, false, false);
      $ret = array('body' =&gt; '');
  
      $callback = function($msg) use ($channel, &amp;$ret) {
          $ret['body'] = $msg-&gt;body;
          /*
          Here I would basic.cancel the consumer if there were no messages in the queue
          */
      };
  
      $channel-&gt;basic_consume(QUEUE_NAME, 'tag', false, true, false, false, $callback);
  
      if (count($channel-&gt;callbacks)) {
          $channel-&gt;wait(); // blocks here...
      }
  
      return $ret;
  </code></pre>