Ask coding questions

← Back to all posts
Flask SocketIO
ArchieMaclean (932)

When I try to run this, it gives the following error:

WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance.

When I try adding gevent/gevent-websocket to my dependencies, it just freezes when it tries to install it.
When I try adding eventlet to my dependencies, it installs it but when I run it it doesn't do anything, just freezes.

This works fine when I run it locally; I think this has something to do with Heroku being evil.

Thank you :)

Answered by NotTani (80) [earned 5 cycles]
View Answer
Comments
hotnewtop
NotTani (80)

Basically, long story short, Flask has its own development server. It is decent in many ways, but it is not asynchronous, so it cannot accept WebSockets. It can, however, accept Socket.IO, because it does not solely rely on WebSockets. Your application will work just fine, but it may be noticeably slower/less efficient. In order for gevent or eventlet to work, you need to use a server that is not the development one (such as gunicorn). If you are running this on repl.it, you just have to deal with this, unfortunately, because it is difficult to run any 3rd party servers. If your deploying to Heroku, have a look at their tutorial on Flask and WebSockets, which uses a different library than you, but I'm sure you can adapt it.

Also, unrelatedly, to get proper web output, change line 30 from socketio.run(app) to socketio.run(app, host='0.0.0.0').

BaconErie (6)

@NotTani Sorry that I'm one year late, and this is sort of off topic, but do you know anyway to install 3rd party servers like nginx on Replit, or is it impossible? I'm trying to use gunicorn and nginx with Flask on Replit, and although gunicorn works, I'm not able to install nginx.

ArchieMaclean (932)

@BaconErie Replit released a beta version of Nix recently that allows you to install anything from the Nix repository. Nginx is in the repository list so you should be able to add it.

Here's a barebones installation to start you off: https://replit.com/@ArchieMaclean/nginx-test

BaconErie (6)

@ArchieMaclean Ah ok, I'll go check that out. Thank you!