title: Flask-SocketIO redis subscribe
body: <p>I'm using <a href="https://github.com/miguelgrinberg/Flask-SocketIO" rel="nofollow noreferrer">https://github.com/miguelgrinberg/Flask-SocketIO</a> to implement a WebSocket server.</p>
  
  <p>I need to receive a message from another process (only subscribe) and emit for clients in a specific room.</p>
  
  <p>But, when i try to send the message, i got this error:</p>
  
  <blockquote>
    <p>Could not send message to home room: Working outside of request context.</p>
  </blockquote>
  
  <p>Here is my code:</p>
  
  <pre><code>from flask import Flask, request
  from flask_socketio import SocketIO, join_room, leave_room, send, rooms
  import json
  import eventlet
  import logging
  import redis
  import threading
  
  FORMAT = '%(asctime)-15s - %(message)s'
  logging.basicConfig(format=FORMAT)
  log = logging.getLogger(__name__)
  
  app = Flask(__name__)
  app.config['SECRET_KEY'] = 'secret!'
  socketio = SocketIO(app, async_mode='eventlet')
  
  .
  .
  .
  
  def _send_task_message():
      try:
          send(json.dumps({"type":"UPDATE_TASK"}), room='home')
      except Exception as e:
          log.error('Could not send message to home room: %s' % str(e)) 
  
  class Listener(threading.Thread):
      def __init__(self, r, channels):
          threading.Thread.__init__(self)
          self.daemon = True
          self.redis = r
          self.pubsub = self.redis.pubsub()
          self.pubsub.psubscribe(channels)
  
      def work(self, item):
          if isinstance(item['data'], bytes):
              try:
                  msg = item['data'].decode('utf-8')
                  decode_msg = json.loads(msg)                
                  if decode_msg['type'] == 'UPDATE_TASK':
                      _send_task_message()
              except ValueError as e:
                  log.error("Error decoding msg to microservice: %s", str(e))
  
      def run(self):
          for item in self.pubsub.listen():
              self.work(item)
  
  
  if __name__ == '__main__':
  
      r = redis.Redis()
      client = Listener(r, ['/bobguarana/socketio'])
      client.start()
  
      socketio.run(debug=True, app=app, port=8080)
  </code></pre>