1

I currently have a project which uses Flask-SocketIO as the backend and a separate client that interacts with the Flask-SocketIO server. Everything is working when I run the server on my local machine. However, when I run the Flask-SocketIO server on a remote server via SSH and try connecting to the host IP address, the connection doesn't seem to work.

The flask-socketio server seems to run fine on the remote server machine as I can see things being printed into the console (running some machine learning models with tensorflow), but I just don't seem to be able to connect to it from the client.

So my main questions are:

  1. What could be the reason why I'm unable to connect to the server remotely?

  2. How exactly can I set up load balancing with nginx? I have looked at the documentation (https://flask-socketio.readthedocs.io/en/latest/#deployment) but I'm still unsure as to where to start.

Currently the project is configured to use the default server, using

if __name__ == '__main__':
    socketio.run(app, port=5000)

I connect to it using

const socket = io('http://[ip address]:5000');

It works when I do

const socket = io('http://localhost:5000');

My server is hosted with DigitalOcean running Ubuntu

Any help would be greatly appreciated!

Charles Landau
  • 4,187
  • 1
  • 8
  • 24
Ray A.
  • 325
  • 1
  • 4
  • 12

1 Answers1

3

On production it is recommended to use a proper application server/reverse proxy combo such as Gunicorn or uWSGI as described in the documentation, but to answer your question, you should have passed the host argument to the .run() method as it defaults to 127.0.0.1:

https://flask-socketio.readthedocs.io/en/latest/#flask_socketio.SocketIO.run

host – The hostname or IP address for the server to listen on. Defaults to 127.0.0.1.

Which means that it will only listen on the local loopback interface and not the Internet interface. You should change it to the public IP address of the VPS, or to 0.0.0.0 to listen on all interfaces. For example:

socketio.run(app, host='0.0.0.0', port=5000)
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • Looks like the server works with gunicorn using this command: `gunicorn --bind 0.0.0.0:5000 --worker-class eventlet -w 1 wsgi:app` I have a file called wsgi.py which simply contains `from app import app if __name__ == "__main__": app.run() ` Now just need to configure load balancing with nginx – Ray A. Dec 12 '18 at 07:33