Skip to main content

Flask-uWSGI-WS

A high-performance WebSocket extension for Flask applications powered by uWSGI. Compatible with Python 3.12 and newer versions.

Example Usage

from flask import Flask
from flask_uwsgi_ws import WebSocket

app = Flask(__name__)
ws = WebSocket(app)

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

if __name__ == '__main__':
    app.run(debug=True, threads=16)

Installation

To install Flask-uWSGI-WS, you'll need to install uWSGI with SSL support first:

For Ubuntu/Debian:

CFLAGS="-I/usr/include/openssl" LDFLAGS="-L/usr/lib/x86_64-linux-gnu" UWSGI_PROFILE_OVERRIDE=ssl=true pip install --no-cache-dir uwsgi --no-binary :all:

For macOS (Apple Silicon):

CFLAGS="-I/opt/homebrew/opt/openssl@3/include" \
LDFLAGS="-L/opt/homebrew/opt/openssl@3/lib" \
UWSGI_PROFILE_OVERRIDE=ssl=true pip install --no-cache-dir uwsgi --no-binary :all:

Then install Flask-uWSGI-WS:

pip install flask-uwsgi-ws

Deployment

You can use uWSGI's built-in HTTP router to get up and running quickly:

$ uwsgi --master --http :8080 --http-websockets --wsgi-file app.py

...or call app.run, passing uwsgi any arguments you like:

app.run(debug=True, host='localhost', port=8080, master=true, processes=8)

Using with Gevent

uWSGI supports several concurrency models, including Gevent. To use Gevent, import GeventWebSocket:

from flask_uwsgi_ws import GeventWebSocket

Then run uWSGI with the gevent loop engine:

$ uwsgi --master --http :8080 --http-websockets --gevent 100 --wsgi-file app.py

...or in your code:

app.run(debug=True, gevent=100)

Development

To use Flask's interactive debugger, install werkzeug's DebuggedApplication middleware:

from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)

Then run uWSGI with a single worker:

$ uwsgi --master --http :8080 --http-websockets --wsgi-file --workers 1 --threads 8 app.py

If you use app.run(debug=True), Flask-uWSGI-WS will do this automatically.

WebSocket API

Flask-uWSGI-WS handles the WebSocket handshake and provides a websocket client with the following methods:

  • websocket.recv() - Receive a message
  • websocket.send(msg) - Send a message
  • websocket.send_binary(msg) - Send a binary message
  • websocket.recv_nb() - Non-blocking receive
  • websocket.send_from_sharedarea(id, pos) - Send from shared memory area
  • websocket.send_binary_from_sharedarea(id, pos) - Send binary from shared memory area

Metadata

Release files for flask-uwsgi-ws 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-uwsgi-ws 0.4.0
File Size Uploaded Provenance Actions
flask_uwsgi_ws-0.4.0.tar.gz 13.7 kB Without provenance Details for flask_uwsgi_ws-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-uwsgi-ws 0.4.0
File Interpreter ABI Platform Actions
flask_uwsgi_ws-0.4.0-py3-none-any.whl Python 3 none any Details for flask_uwsgi_ws-0.4.0-py3-none-any.whl

Total release size: 23.9 kB

Release files / flask_uwsgi_ws-0.4.0.tar.gz

Download URL flask_uwsgi_ws-0.4.0.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fe3a104eceb4caeefa6b93669b476fc6d8f623b4f6f65b93e294f8ef03a93db7
BLAKE2b-256 checksum
How to use checksums
7ec3955d5bf1f6288ac5ad9035442603ca0076f967e3e60b1e4a5633b5bf502b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.0

Release files / flask_uwsgi_ws-0.4.0-py3-none-any.whl

Download URL flask_uwsgi_ws-0.4.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf7ecc251e78a7500a85f067c823d0ad78fb3b8d1c80a8125319e0a8423682ad
BLAKE2b-256 checksum
How to use checksums
51582615e149d192004632abef18c8944c39c847c56096bfaf16c8a4387365d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.32.0

Security for flask-uwsgi-ws 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page