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
flask_uwsgi_ws-0.4.0.tar.gz 13.7 kB Details

Built distribution (wheel)

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

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

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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