Search code examples
javascriptpythonflaskherokusocket.io

Python flask server on Heroku


i have a python (flask) server, it works locally, but i want to deploy my flask server (socketio) on Heroku, so basically the final situation is:

  1. Flask server (socketio on Heroku)
  2. Client js on host like (000webhost)

but when i deploy server it does not work

socketio = SocketIO(app, cors_allowed_origins='*')
porta = int(os.environ.get('PORT', 5000))
if __name__ == '__main__':
socketio.run(app, port=porta)

Procfile:

web: python3 server.py

Solution

  • You need to bind to '0.0.0.0' instead of the default ('localhost'). This allows the socketio server to accept external connections.

    socketio.run(host='0.0.0.0', port=porta)
    

    You can see an example of this in a Heroku blog post about Django, another Python web framework: https://blog.heroku.com/python_and_django