Skip to main content

Flask WebSocket extension for uWSGI

Project description

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

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_uwsgi_ws-0.4.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_uwsgi_ws-0.4.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_uwsgi_ws-0.4.0.tar.gz.

File metadata

  • Download URL: flask_uwsgi_ws-0.4.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.32.0

File hashes

Hashes for flask_uwsgi_ws-0.4.0.tar.gz
Algorithm Hash digest
SHA256 fe3a104eceb4caeefa6b93669b476fc6d8f623b4f6f65b93e294f8ef03a93db7
MD5 32f2e6ea623a4486b3fc1f167cba9878
BLAKE2b-256 7ec3955d5bf1f6288ac5ad9035442603ca0076f967e3e60b1e4a5633b5bf502b

See more details on using hashes here.

File details

Details for the file flask_uwsgi_ws-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_uwsgi_ws-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7ecc251e78a7500a85f067c823d0ad78fb3b8d1c80a8125319e0a8423682ad
MD5 d2b0543aa5d612ec2ea704945240e00c
BLAKE2b-256 51582615e149d192004632abef18c8944c39c847c56096bfaf16c8a4387365d9

See more details on using hashes here.

Supported by

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