Search code examples
flaskgeventflask-socketioeventletgevent-socketio

How to deploy flask with socketio to heroku and have it upgrade to websockets


Locally, it works. Socketio upgrades to websocket instead of resorting to polling.

This is obvious from the logs:

...
FYnWEW0ufWGO7ExdAAAA: Received request to upgrade to websocket
FYnWEW0ufWGO7ExdAAAA: Upgrade to websocket successful
...

Upon deploying the application, it partially works when I create a procfile with the content:

web: gunicorn app:app

The issue here is that socketio fails to upgrade to websocket and therefore resorts to polling.

Here is a gif showcasing that it in production doesn't upgrade to websockets and resorts to spamming pollings instead


My file structure is

wsgi.py
app.py
Procfile
requirements.txt

This is how I initialize socketio

app = ...
socketio = SocketIO(app,
    logger=True,
    engineio_logger=True,
    cors_allowed_origins="*"
)
if __name__ == "__main__":
    socketio.run(app, debug=False, port=5000)

Notice Im not setting async_mode, which was the issue for this SO-question


How do I deploy my flask app with socketio to Heroku and have it upgrade to websockets?

I think the issue is that Im just not using the right procfile command to start the application in deployment.


Solution

  • Having a procfile with the content

    web: gunicorn --worker-class eventlet -w 1 wsgi:app

    Did the job.


    Also, it is important that your dyno is set to "ON" enter image description here