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 server is compatible with Werkzeug (Flask's development web server) and Gunicorn.
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. At this time the supported servers are:
- Werkzeug (Flask development server)
- Gunicorn
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-sock-0.1.0.tar.gz
.
File metadata
- Download URL: flask-sock-0.1.0.tar.gz
- Upload date:
- Size: 3.9 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45b5dcd5823674b4f7ac1cbcadda5b4b2bdfb2c1087a7aeb8b840c627c4c207b |
|
MD5 | 170aa96e3292e96e8d7ccc1a6dc3ef1b |
|
BLAKE2b-256 | cfab726ea325e0bcf218e42f87e1d1ae1a72a5ed68fae53c4533e197bb687544 |
File details
Details for the file flask_sock-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: flask_sock-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.6 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 323a23fba0c0b08c8e3a936aa51f5327771228cdcc1ff824534895c99ba1f182 |
|
MD5 | 98f06cacb0bf37fd5d5d0b041a156684 |
|
BLAKE2b-256 | 85c422c8a49c5d1f5ff97eca6002569fc88679267290162277f7bd03b76d6131 |