miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
274 stars 24 forks source link

flask-sock in docker-gunicorn environment always returns 404 #28

Closed ch-IAE-HSN closed 1 year ago

ch-IAE-HSN commented 2 years ago

Hi Miguel,

i have an issue with flask-sock in an containerized application. As I tested my app in a local environment, everything works as expected, the javascript in my html-doc connects properly and the communication runs continuously.

But now I switched to an containerized environment using gunicorn as webserver started by supervisor and now the server returns always a 404 as the html doc calls the websocket. I tried several configs to figure out what's happening, but I have no more ideas what to do.

Here are the crucial parts of code, config and log:

app.py

...
app = flask.Flask(
    __name__,
    static_folder = '/www/static'
)

ws = flask_sock.Sock()
...

def create_app():
    ...
    app.config['SOCK_SERVER_OPTIONS'] = {
        'ping_interval' : 5
    }
    ...
    ws.init_app(app)
    ...
    from .views import default
    ... 
    return app

default.py

@ws.route('/api/ws')
def wsapi(ws):
    app.logger.info('ws call')
    while True:
        time.sleep(1)
        ws.send('Hello')
        app.logger.info('ws cycle')

index.html

            const ws = new WebSocket('ws://'+location.host+'/api/ws');

            ws.addEventListener('open', ev => {
                console.log('WS OPENED!');
            });

            ws.addEventListener('close', ev => {
                console.log('WS CLOSED!');
            });

            ws.addEventListener('message', ev => {
                console.log(ev.data)
            });

supervisord.conf

...
[program:gunicorn]
command=/www/venv/bin/gunicorn wsgi:app -b :80 --reload -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker --workers 4 --threads 100
directory=/www/app
user=www-data
group=www-data
autostart=true
autorestart=true
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
...

log (generated with app.before_request & app.after_request)

[2022-05-12 08:23:59,915] INFO in app: <Request 'http://10.1.7.3:9500/api/ws' [GET]>
[2022-05-12 08:23:59,917] INFO in app: <Response streamed [404 NOT FOUND]>

Maybe you have some hints for me, how to use your flask-extension properly.

Cheers, Carsten

miguelgrinberg commented 2 years ago

You aren't showing enough of your set up. Your Gunicorn is on port 80, yet you are connecting on port 9500. Why is that? Also what's the point of using supervisor when you have a docker deployment?

ch-IAE-HSN commented 2 years ago

I connect through port 9500, as this is the host port of my docker test node:

docker run -d -p 9500:80 -p 9501:22 --restart=always -v static:/www/static:ro -v app:/www/app:ro --name kimgui kimgui:latest

In this development env I also run an ssh server and this 2 services were started with supervisor:

[supervisord] nodaemon=true

[program:sshd] command=/usr/sbin/sshd -D -o ListenAddress=0.0.0.0

[program:gunicorn] command=/www/venv/bin/gunicorn wsgi:app -b :80 --reload -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker --workers 4 --threads 100 directory=/www/app user=www-data group=www-data autostart=true autorestart=true stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 stderr_logfile=/dev/stderr stderr_logfile_maxbytes=0

miguelgrinberg commented 2 years ago

Try moving the import of your default module outside of the factory function, or at least move it above the ws.init_app() call.

ch-IAE-HSN commented 2 years ago

Strange! As the ws.route is registered to the blueprint, the endpoint never appears in the url_map of the flask-app. As I pass the app as a constructor arg to the Sock-class, everything works as expected...

miguelgrinberg commented 2 years ago

Yeah, you were doing things in a strange order that this extension does not currently support. I'll look into making things work for this use case.

miguelgrinberg commented 1 year ago

This should work, indirectly after 3bfacdee99006a6db170d12b2988d41f4376e210 was introduced a while ago.