1

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.

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43

1 Answers1

0

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

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43
  • I've had some issues with sockets before. It completely took over my dyno and blocked HTTP requests. What does --worker-class do, and does it not make your website really slow? – odinchess Jan 30 '22 at 21:49
  • @odinchess I am not sure, but I haven't had any issues with it. By the way, this is where I learnt about command: https://flask-socketio.readthedocs.io/en/latest/deployment.html#gunicorn-web-server – Sebastian Nielsen Jan 30 '22 at 21:56
  • It seems to work with flask-sse too now! Thanks for the help. I used the second command (e.g. gevent instead of eventlet). Do you get H12 or H15 errors for server timeout? – odinchess Jan 31 '22 at 05:11
  • @odinchess Nope, no errors. – Sebastian Nielsen Jan 31 '22 at 10:39
  • I had 1 worker just like yours and it was not enough to handle my incoming traffic. How did you scale when you have heavy track to your application? – ApplePie Jan 31 '23 at 17:41