https://github.com/ch-iv/flask-websockets
WebSockets extension with channels for Flask
https://github.com/ch-iv/flask-websockets
flask flask-websockets python websocket websockets
Last synced: about 2 months ago
JSON representation
WebSockets extension with channels for Flask
- Host: GitHub
- URL: https://github.com/ch-iv/flask-websockets
- Owner: ch-iv
- License: mit
- Created: 2024-07-03T02:16:39.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-19T21:58:31.000Z (almost 2 years ago)
- Last Synced: 2025-02-20T14:15:57.138Z (over 1 year ago)
- Topics: flask, flask-websockets, python, websocket, websockets
- Language: Python
- Homepage: https://flaskwebsockets.readthedocs.io
- Size: 207 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# flask-websockets


[](https://flaskwebsockets.readthedocs.io/en/latest/?badge=latest)

[](https://github.com/astral-sh/ruff)
[](https://github.com/astral-sh/ruff)
flask-websockets is an extension library for Flask, a popular web micro-framework. It adds real-time communication capabilities to your Flask application. flask-websockets implements the WebSocket protocol and allows for low-level control over the connections, as well as a high-level API for subscribing connections to rooms.
flask-websockets supports most popular HTTP WSGI servers such as Werkzeug, Gunicorn, Eventlet and Gevent.
## Example Usage
```python
import time
from threading import Thread
from flask import Flask
from flask_websockets import WebSocket, WebSockets
app = Flask(__name__)
websockets = WebSockets(app)
@websockets.route("/ws")
def websocket_route(ws: WebSocket) -> None:
with websockets.subscribe(ws, ["server_time"]):
for data in ws.iter_data(): # keep listening to the websocket so it doesn't disconnect
pass
def publish_server_time() -> None:
while True:
websockets.publish(str(time.time()), ["server_time"])
time.sleep(1)
Thread(target=publish_server_time).start()
app.run(host="localhost", port=6969)
```