Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SocketIO Not Working On Apache/WSGI

I have used the following example code from http://blog.miguelgrinberg.com/post/easy-websockets-with-flask-and-gevent/page/4 and this works fine when I run it using the test server e.g. python myapp.py I can connect to it and send messages

from flask import Flask, render_template
from flask.ext.socketio import SocketIO, emit

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('my event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']})

@socketio.on('my broadcast event', namespace='/test')
def test_message(message):
    emit('my response', {'data': message['data']}, broadcast=True)

@socketio.on('connect', namespace='/test')
def test_connect():
    emit('my response', {'data': 'Connected'})

@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected')

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

The problem is when I move this same code to a server that is using Apache to serve Flask I get errors.

RuntimeError: You need to use a gevent-socketio server.

Config file for Apache host is:

 WSGIApplicationGroup %{GLOBAL}
 WSGIScriptAlias / /var/www/public/flaskApp/flaskApp.wsgi

 <Location /var/www/public/flaskApp/flaskApp/>
    Order allow,deny
    Allow from all
</Location>

Is it possible to run SocketIO/Flask and have it work through Apache?

like image 445
user4143585 Avatar asked Oct 31 '22 05:10

user4143585


1 Answers

Your /var/www/public/flaskApp/flaskApp.wsgi file that Apache is running your app through doesn't use a socketio-capable server.

The tutorial you're reading states

The extension is initialized in the usual way, but to simplify the start up of the server a custom run() method is provided by the extension. This method starts gevent, the only supported web server. Using gunicorn with a gevent worker should also work.

The uWSGI documentation has a section on running in gevent mode, but Miguel commented:

uwsgi does not work with this extension either, because it does not allow a custom gevent loop to be used. Gunicorn does work, the command is in the documentation.

So, Gunicorn. From the docs:

An alternative is to use gunicorn as web server, using the worker class provided by gevent-socketio. The command line that starts the server in this way is shown below:

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app

In short, ensure that you're running with something that provides the gevent worker.

like image 155
Celeo Avatar answered Nov 13 '22 22:11

Celeo