Skip to main content

WebSocket support for Flask

Project description

flask-sock

WebSocket support for Flask (and possibly other WSGI frameworks). What makes this package different than other WebSocket servers is that it does not require a greenlet based server (gevent, eventlet) to work.

This WebSocket implementation is compatible with the following synchronous web servers:

  • Werkzeug (Flask's development web server)
  • Gunicorn with the gthread worker

In addition to the servers above, the following asynchronous web servers are supported when the Python standard library is monkey patched:

  • Eventlet's WSGI server
  • Gevent's WSGI server
  • Gunicorn with the eventlet worker
  • Gunicorn with the gevent worker

Installation

pip install flask-sock

Example

from flask import Flask, render_template
from flask_sock import Sock

app = Flask(__name__)
sock = Sock(app)


@sock.route('/echo')
def echo(ws):
    while True:
        data = ws.receive()
        ws.send(data)

Running

To run an application that uses this package, you need to use a supported web server.

Running with Werkzeug

Werkzeug supports WebSocket routing in version 2, which at this time hasn't been officially released. You can install a supported release candidate with the following command:

pip install "werkzeug>=2.0.0rc3"

To run your application use the normal method that you always use. Both the flask run and app.run() methods of starting the Flask application should work.

Running with Gunicorn

To use this package with Gunicorn you need to keep in mind that each active WebSocket client will use up a worker. The most practical way to run a WebSocket server is to use the --threads option to allocate the number of clients that you need:

gunicorn -b :5000 --threads 100 module:app

It is also okay to use multiple workers, each with a number of allocated threads:

gunicorn -b :5000 --workers 4 --threads 100 module:app

Running with eventlet

To serve your application with the eventlet WSGI server you can use the following script:

import eventlet
eventlet.monkey_patch()

from eventlet import wsgi
from module import app

wsgi.server(eventlet.listen(('', 5000)), app)

It is also possible to use Gunicorn's eventlet worker:

gunicorn -b :5000 --worker-class eventlet module:app

Gunicorn's eventlet worker handles a maximum of 1000 concurrent requests in a single worker process by default. The maximum number of concurrent requests can be changed with the --worker-connections option. The number of workers can be changed with the --workers option.

Running with gevent

To serve your application with the gevent WSGI server you can use the following script:

from gevent import monkey
monkey.patch_all()

from gevent.pywsgi import WSGIServer
from module import app

WSGIServer(('127.0.0.1', 5000), app).serve_forever()

It is also possible to use Gunicorn's gevent worker:

gunicorn -b :5000 --worker-class gevent module:app

Gunicorn's gevent worker handles a maximum of 1000 concurrent requests in a single worker process by default. The maximum number of concurrent requests can be changed with the --worker-connections option. The number of workers can be changed with the --workers option.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-sock-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

flask_sock-0.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file flask-sock-0.2.0.tar.gz.

File metadata

  • Download URL: flask-sock-0.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.8.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for flask-sock-0.2.0.tar.gz
Algorithm Hash digest
SHA256 63cd753b4205669b6515ed5fac2cd6db898a17f9d9d132f9d9783eaa7d8ce8db
MD5 16c873753c895ac6b5bc35b6303a490e
BLAKE2b-256 bde0ad63c3f4dd90736d9b947ac38ba1305252d78a5abf65c692cd03b180b9fd

See more details on using hashes here.

File details

Details for the file flask_sock-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: flask_sock-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.8.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.6

File hashes

Hashes for flask_sock-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef1c242107e992ebff6acbd203d34d24184f9e4a23bd4c9a598380a548a46173
MD5 ef2119c1714dfbbf5371c4d31c9f4100
BLAKE2b-256 a900da5cd78d87ca956a0e618f0436120f1c3ce3316bd4b37c3686c15ba2272f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page