miguelgrinberg / Flask-SocketIO

Socket.IO integration for Flask applications.
MIT License
5.35k stars 889 forks source link

Always polling, never Websockets #1288

Closed seanieb closed 4 years ago

seanieb commented 4 years ago

I've a Flask app that uses blue prints and runs in a docker container. The problem I'm having, is that the connection never upgrades to WS. I've turned off development mode and eventlet is installed.

It's started using a serve.py file:

from app import create_app, config, socketio

app = create_app(config.base_config)

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

The app is not run in the development mode. HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io();
    socket.on('connect', function() {
        socket.emit('my event', {data: 'I\'m connected!'});
    });
</script>

I refresh this to test if it establishes a WS. The GET request return Server: Werkzeug/1.0.1 Python/3.8.3

Requirements

arrow==0.15.6
blinker==1.4
cssmin==0.2.0
email_validator==1.1.1
eventlet==0.25.2
Faker==4.1.0
Flask-Assets==2.0
Flask-Babel==1.0.0
Flask-Bcrypt==0.7.1
Flask-Limiter==1.3.1
Flask-Login==0.5.0
Flask-Mail==0.9.1
Flask-Migrate==2.5.3
flask-postmark==0.2.0
flask-redis==0.4.0 
Flask-RQ2==18.3
Flask-Script==2.0.6
Flask-SocketIO==4.3.0
Flask-SQLAlchemy==2.4.1
Flask-WTF==0.14.3
Flask==1.1.2
gunicorn==20.0.4
itsdangerous==1.1.0
Jinja2==2.11.2
jsmin==2.2.2
jsonschema==3.2.0
MarkupSafe==1.1.1
psycopg2==2.8.5
pytest==5.4.2
python-dateutil==2.8.1
redis==3.5.2
requests==2.23.0
sentry-sdk[flask]==0.14.4
SQLAlchemy==1.3.17
Werkzeug==1.0.1 
WTForms==2.3.1

Logs

app_1        |  * Serving Flask app "serve.py"
app_1        |  * Environment: production
app_1        |    WARNING: This is a development server. Do not use it in a production deployment.
app_1        |    Use a production WSGI server instead.
app_1        |  * Debug mode: off
app_1        | Server initialized for threading.
app_1        |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
app_1        | 758be8cb3f444d5ab42453fa831848c9: Sending packet OPEN data {'sid': '758be8cb3f444d5ab42453fa831848c9', 'upgrades': [], 'pingTimeout': 60000, 'pingInterval': 25000}
app_1        | 758be8cb3f444d5ab42453fa831848c9: Sending packet MESSAGE data 0
app_1        | 172.18.0.1 - - [29/May/2020 08:52:19] "GET /socket.io/?EIO=3&transport=polling&t=N9VeT5s HTTP/1.1" 200 -
app_1        | 758be8cb3f444d5ab42453fa831848c9: Received packet MESSAGE data 2["my event",{"data":"I'm connected!"}]
app_1        | received event "my event" from 758be8cb3f444d5ab42453fa831848c9 [/]
app_1        | emitting event "message" to all [/]
app_1        | 758be8cb3f444d5ab42453fa831848c9: Sending packet MESSAGE data 2["message","ack"]
app_1        | 172.18.0.1 - - [29/May/2020 08:52:20] "GET /socket.io/?EIO=3&transport=polling&t=N9VeT6D&sid=758be8cb3f444d5ab42453fa831848c9 HTTP/1.1" 200 -
app_1        | 172.18.0.1 - - [29/May/2020 08:52:20] "POST /socket.io/?EIO=3&transport=polling&t=N9VeT6B&sid=758be8cb3f444d5ab42453fa831848c9 HTTP/1.1" 200 -
app_1        | 758be8cb3f444d5ab42453fa831848c9: Received packet PING data None
app_1        | 758be8cb3f444d5ab42453fa831848c9: Sending packet PONG data None
app_1        | 172.18.0.1 - - [29/May/2020 08:52:46] "POST /socket.io/?EIO=3&transport=polling&t=N9VeZUb&sid=758be8cb3f444d5ab42453fa831848c9 HTTP/1.1" 200 -
app_1        | 172.18.0.1 - - [29/May/2020 08:52:46] "GET /socket.io/?EIO=3&transport=polling&t=N9VeT6V&sid=758be8cb3f444d5ab42453fa831848c9 HTTP/1.1" 200 -
miguelgrinberg commented 4 years ago

Well, this is a problem:

Server initialized for threading.

For some reason your application isn't running with the eventlet server. Are you passing the async_mode argument into your SocketIO() class? Try removing it if you are, so that the server selects the async mode on its own.

seanieb commented 4 years ago

Are you passing the async_mode argument into your SocketIO() class?

No.

After doing some simplification it was related to how I was starting the app using the flask command in my docker/docker compose setup. Using command: python -m flask run --host 0.0.0.0 --port 5000 in docker compose fixed this. Specifying the file in the envar for the flask command doesn't seem to work.

Thank you so much for time and help. Your projects and writing are really fantastic.