title: Opening a separate socket while running flask socketio server
body: <p>I'm trying to write a Flask app where clients connect through a socket on a simple website, and which also communicates to a different process (on the same machine), also with a socket.</p>
  
  <p>I'm probably being silly but am new to sockets and having a hard time understanding documentation/other SO posts on it. Sorry if it's a bad question!</p>
  
  <p>So I have three files (in this simplified version):</p>
  
  <p><code>index.html</code>, which opens a socket to the Flask server on port 5000.</p>
  
  <p><code>app.py</code>, which runs the flask app as a socketio object on port 5000 and connects to the backend on port 5500.</p>
  
  <p><code>backend.py</code>, which opens a socket on port <code>5500</code> and just prints out whatever it receives.</p>
  
  <p>My HTML/JS is pretty simple (using socket.io):</p>
  
  <pre><code> s = io.connect('ws://localhost:5000');
   s.emit('message', {'data': 'Hello world'});
  </code></pre>
  
  <p>My goal is to get "Hello world" to pass through the flask app and to the backend server. I think the backend server is fine, it just waits to receive data then prints it out.</p>
  
  <pre><code># backend.py, server
  HOST = '127.0.0.1'
  PORT = 5500
  
  with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
      s.bind((HOST, PORT))
      s.listen(1)
      conn, addr = s.accept()
      with conn:
          print('Connected to ' + repr(addr))
          while True:
              print('Receiving data...')
              data = conn.recv(1024).decode()
              print('Data received: ' + data)
              if data == 'STOP' or not data:
                  break
  
  print('Exiting server')
  </code></pre>
  
  <p>And here's my app.py:</p>
  
  <pre><code># app.py, client and server
  HOST = '127.0.0.1'
  PORT = 5500
  
  app = Flask(__name__)
  socketio = SocketIO(app)
  backend_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  backend_socket.connect((HOST, PORT))
  
  @app.route('/')
  def main():
      return render_template('index.html')
  
  @socketio.on('message')
  def message(msg):
      print('received message ' + msg['data'])
      backend_socket.sendall(bytes(msg['data']))
  
  if __name__ == '__main__':
      socketio.run(app, port=5000, debug=True)
  </code></pre>
  
  <p><strong>What's the issue</strong> : When the message is emitted, the .on('message') route is properly called and 'received message Hello world' is printed out on the app.py process. However, the sendall doesn't work. On the backend process, it gets hung up on the <code>conn.recv</code> call.</p>
  
  <p>I've googled around and it seems like this has to do with Flask changing how sockets are handled or something but to be honest I couldn't make heads or tail of the answers I found.</p>
  
  <p>I'd really like to do this without using a separate thread (it doesn't make sense to me that you'd need a thread when I already have separate processes) but whatever works is fine. More interested in understanding what the issue is than fixing it!</p>