Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best option currently available for websockets with flask on python3 [closed]

currently i see the following libraries available for using websockets with flask:

  1. flask-sockets (possibly obsolete)
  2. flask-SocketIO
  3. gevent-websockets (above 2 depend on this)

However gevent-websockets is not yet python3 compatible(atleast it is not install-able via pip). Which makes the other 2 unusable as well.

Is there any other alternative for using websockets with flask? Or is tornado the best choice?

like image 791
Ishan Khare Avatar asked Oct 04 '15 16:10

Ishan Khare


1 Answers

First of all, note that Flask-SocketIO is different from the other two. It wraps the Socket.IO protocol, not raw WebSocket. This enables it to support a WebSocket-like interface emulated with HTTP long-polling for clients that do not support the WebSocket protocol.

Speaking of Flask-SocketIO, note that this extension is about to receive a major overhaul.

The current official release (0.6) depends on gevent, gevent-socketio and gevent-websocket. Unfortunately gevent-socketio as a project has long been dead and is stuck on a very old version of the Socket.IO protocol, which forces you to use an ancient Javascript client.

The upcoming Flask-SocketIO release 1.0 is a complete rewrite that is mostly compatible with the 0.6 release, but it has support for Python 2 and Python 3 and sports a design that allows different mechanisms to be used for networking and concurrency. The gevent-socketio dependency from version 0.6 is replaced with two new packages called python-socketio and python-engineio, which implement the current version of the Socket.IO protocol, and support all the official Socket.IO clients (Javascript, Swift and C++).

As far as low-level dependencies, you have basically three options:

  1. Eventlet

    Eventlet is by far the most performant and stable option, on both Py2 and Py3. WebSocket support is native to eventlet, so just installing eventlet in your virtualenv enables Flask-SocketIO to work in the most optimal configuration.

  2. gevent and gevent-websocket

    If for any reason you prefer gevent, that also works, but in my tests it does not perform as good as eventlet. If you want to have access to WebSocket when using gevent, then you also need to install package gevent-websocket. Both gevent and gevent-websocket have been ported to Python 3, but it is all too recent.

  3. Werkzeug (or any other multi-threaded WSGI compatible server)

    You can also use Flask's own web server. In this mode, the extension only works in long-polling mode. Performance is obviously terrible compared to the other two, but for development and debugging it is nice to be able to get everything working without having to run eventlet or gevent, with all the complexities they bring.

As of today (10/4/2015), there is a fully functional beta release of the new Flask-SocketIO extension. An official release is coming soon. You can install the beta release with:

pip install flask-socketio==1.0b1

The code is available on the 1.0 branch of the git repo: https://github.com/miguelgrinberg/Flask-SocketIO/tree/v1.0.

Disclaimer: I'm sure this is clear by now, but just in case, note that I'm the author of Flask-SocketIO, python-socketio and python-engineio.

like image 133
Miguel Avatar answered Nov 03 '22 18:11

Miguel