title: Flask + RabbitMQ + SocketIO - forwarding messages
body: <p>I'm facing problem in emiting messages from RabbitMQ to User via SocketIO.</p>
  
  <p>I have Flask application with <a href="https://github.com/miguelgrinberg/Flask-SocketIO" rel="noreferrer">SocketIO integration</a>.
  Current user flow seems like <img src="https://i.stack.imgur.com/Af3nN.png" alt="this"></p>
  
  <p>The problem is i'm not able to set up RabbitMQ listener which forward messages to browser via SocketIO. Every time i'm getting different error. Mostly is that connection is closed, or i'm working outside of application context. </p>
  
  <p>I tried many approaches, here is my last one.</p>
  
  <pre><code># callback 
  def mq_listen(uid):
      rabbit = RabbitMQ()
      def cb(ch, method, properties, body, mq=rabbit):
          to_return = [0]  # mutable
          message = Message.load(body)
          to_return[0] = message.get_message()
  
          emit('report_part', {"data": to_return[0]})
  
      rabbit.listen('results', callback=cb, id=uid)
  
  # this is the page, which user reach
  @blueprint.route('/report_result/&lt;uid&gt;', methods=['GET'])
  def report_result(uid):
  
      thread = threading.Thread(target=mq_listen, args=(uid,))
      thread.start()
  
       return render_template("property/report_result.html", socket_id=uid)
  </code></pre>
  
  <p>where rabbit.listen method is abstraction like:</p>
  
  <pre><code>def listen(self, queue_name, callback=None, id=None):
      if callback is not None:
          callback_function = callback
      else:
          callback_function = self.__callback
      if id is None:
          self.channel.queue_declare(queue=queue_name, durable=True)
          self.channel.basic_qos(prefetch_count=1)
          self.consumer_tag = self.channel.basic_consume(callback_function, queue=queue_name)
          self.channel.start_consuming()
      else:
          self.channel.exchange_declare(exchange=queue_name, type='direct')
          result = self.channel.queue_declare(exclusive=True)
          exchange_name = result.method.queue
          self.channel.queue_bind(exchange=queue_name, queue=exchange_name, routing_key=id)
          self.channel.basic_consume(callback_function, queue=exchange_name, no_ack=True)
          self.channel.start_consuming()
  </code></pre>
  
  <p>which resulted into</p>
  
  <pre><code>RuntimeError: working outside of request context
  </code></pre>
  
  <p>I will be happy for any tip or example of usage.</p>
  
  <p>Thanks a lot</p>