From 5a350c70fdc475b2d1f6b2c9e4d63e1c52c57b19 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 8 Aug 2015 20:15:15 -0700 Subject: [PATCH] Added gevent and threading support, added disconnect function --- docs/index.rst | 243 ++++++++++++++++++++++++++-------- example/app.py | 4 +- flask_socketio/__init__.py | 110 ++++++++------- flask_socketio/test_client.py | 75 ++++++----- setup.py | 2 +- test_socketio.py | 113 ++++++++-------- 6 files changed, 348 insertions(+), 199 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 0b9b3ad7..998936f3 100755 --- a/docs/index.rst +++ b/docs/index.rst @@ -6,26 +6,49 @@ Welcome to Flask-SocketIO's documentation! ========================================== -**Flask-SocketIO** gives Flask applications access to low latency bi-directional communications between the clients and the server. The client-side application can use the `SocketIO `_ Javascript library or any compatible client to establish a permanent connection to the server. +**Flask-SocketIO** gives Flask applications access to low latency +bi-directional communications between the clients and the server. The +client-side application can use the `SocketIO `_ Javascript +library or any compatible client to establish a permanent connection to the +server. Requirements ------------ -Flask-SocketIO is based on `gevent-socketio `_ which in turn depends on `gevent `_ and `gevent-websocket `_. +Since version 1.0, this extension is fully compatible with Python 2.7 and +Python 3.3+, and a few different asynchronous frameworks can be used. At this +time, the most complete implementation uses +`eventlet `_ as a dependency. As an alternative, +`gevent `_ can be used instead of eventlet. The Flask +development server based on Werkzeug can be used as well, with the caveat that +it lacks the performance of the other two options, so it should only be used +to simplify the development workflow. + +On the client-side, the Socket.IO Javascript library is used to establish a +connection to the server. Versions 1.3.5 or newer of the Socket.IO client are +recommended. Versions of the Socket.IO client prior to 1.0 are not supported +anymore. + +Note that older versions of Flask-SocketIO had a completely different set of +requirements. These versions had a dependency on +`gevent-socketio `_ and +`gevent-websocket `_, which are +not used anymore. Current Limitations ~~~~~~~~~~~~~~~~~~~ -- Projects gevent and gevent-socketio only support Python 2.x at this time. There has been some activity in the gevent project towards Python 3 compliance, so support for Python 3 is likely to come in the near future. -- The 1.x releases of the Socket.IO client-side Javascript libraries are not compatible with project gevent-socketio. At this time the Socket.IO client release 0.9.16 has been found to be the most stable. +- Flask-SocketIO can only run in a single worker process at this time. Work is +currently in progress to eliminate this limitation. Initialization -------------- -The following code example shows how to add Flask-SocketIO to a Flask application:: +The following code example shows how to add Flask-SocketIO to a Flask +application:: from flask import Flask, render_template - from flask.ext.socketio import SocketIO + from flask_socketio import SocketIO app = Flask(__name__) app.config['SECRET_KEY'] = 'secret!' @@ -34,11 +57,19 @@ The following code example shows how to add Flask-SocketIO to a Flask applicatio if __name__ == '__main__': socketio.run(app) -The ``init_app()`` style of initialization is also supported. Note the way the web server is started. The ``socketio.run()`` function encapsulates the start up of the gevent web server and replaces the standard Werkzeug development web server, which cannot be used with this extension. However, the Werkzeug debugger and reloader modules are still used when the application is in debug mode. +The ``init_app()`` style of initialization is also supported. Note the way the +web server is started. The ``socketio.run()`` function encapsulates the start +up of the web server and replaces the standard ``app.run()`` standard Flask +development server start up. When the application is in debug mode the +Werkzeug development server is still used and configured properly inside +``socketio.run()``. In production mode the eventlet web server is used if +available, else the gevent web server is used. If eventlet and gevent are not +installed, the Werkzeug development web server is used. -The application must serve a page to the client that loads the Socket.IO library and establishes a connection:: +The application must serve a page to the client that loads the Socket.IO +library and establishes a connection:: - +