I am trying to run a flask app that includes web-sockets and it uses flask_socketio.
My Python code is as follows
from flask import jsonify, request, Flask
from flask_cors import CORS
from flask_socketio import SocketIO
app = Flask(__name__)
socketIo = SocketIO(app, cors_allowed_origins='*', async_mode='gevent')
CORS(app)
data = 5
@socketIo.on('message_emitted')
def handle_message(message):
global data
data = message['value']
# Broadcast the received message to all clients
socketIo.emit('data_changed', { 'value': data })
@app.route('/api/data', methods=['POST'])
def set_data():
global data
data = request.json['value']
socketIo.emit('data_changed', { 'value': data })
return jsonify({'message': 'data is set' })
@app.route('/api/data', methods=['GET'])
def get_data():
return jsonify({'data': { 'value': data } })
@app.route('/')
def home():
return 'Hello for Flask Deployment'
if __name__ == '__main__':
socketIo.run(app=app, debug=True, host='0.0.0.0', port=5000)
When I start my app using gunicorn server by running the command gunicorn -w 3 -k gevent server:app, I get an error
File ".local/lib/python3.10/site-packages/engineio/async_drivers/gevent.py", line 54, in __call__
raise RuntimeError('The gevent-websocket server is not '
RuntimeError: The gevent-websocket server is not configured appropriately. See the Deployment section of the documentation for more information.
Although I did set the configuration async_mode='gevent' in socketIo = SocketIO(app, cors_allowed_origins='*', async_mode='gevent'), I also tried socketio = SocketIO(app, async_mode='gevent', websocket_class=WebSocketHandler).
I checked the documentation https://flask-socketio.readthedocs.io/en/latest/deployment.html#:~:text=gunicorn%20%2Dk%20gevent%20%2Dw%201%20module%3Aapp and I can not see what I am doing wrong.
I tried running my flask-socketio app with gunicorn + gevent using the command gunicorn -w 3 -k gevent server:app but I get the error
File ".local/lib/python3.10/site-packages/engineio/async_drivers/gevent.py", line 54, in __call__
raise RuntimeError('The gevent-websocket server is not '
RuntimeError: The gevent-websocket server is not configured appropriately. See the Deployment section of the documentation for more information.
I was expecting by adding async_mode='gevent', it should work.
Can someone help me running gunicorn with flask-socketio?
Thank you in advance.