heroku-python / flask-sockets

[DEPRECATED] Alternative: https://github.com/miguelgrinberg/flask-sock
MIT License
1.74k stars 167 forks source link

Socket handshake error when using gunicorn #78

Open arliber opened 3 years ago

arliber commented 3 years ago

I have a flask app that processes a web socket stream of audio from Twilio.

The app works fine without gunicorn but when I start it with gunicorn I get only the first message of the socket (connect) and an unsuccessful handshake. Here is how the app looks:

from flask import Flask
from flask_sockets import Sockets
from geventwebsocket.handler import WebSocketHandler
from gevent import pywsgi
...

app = Flask(__name__)
sockets = Sockets(app)
...
@sockets.route('/media')
def media(ws):
    ...
if __name__ == '__main__':
    server = pywsgi.WSGIServer(('', HTTP_SERVER_PORT), app, handler_class=WebSocketHandler)
    server.serve_forever()

When I start the app directly using python flaskapp.py it works ok.

When I start it using gunicorn by writing:

gunicorn -k flask_sockets.worker --bind 0.0.0.0:5055 --log-level=bug flaskapp:app

this is where the connection "hangs" and carries no further than the initial connection, apparently due to the handshake failing.

It's important to note that I haven't "gevent monkey patched" the code, but I'm not sure if it has anything to do with the problem.

Any idea will much be appreciated!

enchant97 commented 3 years ago

Try launching gunicorn with this: gunicorn -k geventwebsocket.gunicorn.workers.GeventWebSocketWorker --bind 0.0.0.0:5055 --log-level=bug flaskapp:app