Sanic Server-Sent Events extension
Project description
Sanic Server-Sent Events extension
A Sanic extension for HTML5 server-sent events support, inspired by flask-sse and aiohttp-sse.
Install
Installation process as simple as:
$ pip install sanic_sse
Example
Server example:
from http import HTTPStatus
from sanic import Sanic
from sanic.response import json, json_dumps
from sanic.exceptions import abort
from sanic_sse import Sse
# This function is optional callback before sse request
# You can use it for authorization purpose or something else
async def before_sse_request(request):
if request.headers.get("Auth", "") != "some_token":
abort(HTTPStatus.UNAUTHORIZED, "Bad auth token")
sanic_app = Sanic()
# The default sse url is /sse but you can set it via init argument url.
Sse(
sanic_app, url="/events", before_request_func=before_sse_request
) # or you can use init_app method
@sanic_app.route("/send", methods=["POST"])
async def send_event(request):
# if channel_id is None than event will be send to all subscribers
channel_id = request.json.get("channel_id")
# optional arguments: event_id - str, event - str, retry - int
# data should always be str
try:
await request.app.sse_send(json_dumps(request.json), channel_id=channel_id)
except KeyError:
abort(HTTPStatus.NOT_FOUND, "channel not found")
return json({"status": "ok"})
if __name__ == "__main__":
sanic_app.run(host="0.0.0.0", port=8000)
Client example (powered by sseclient-py and requests):
import json
import pprint
import requests
import sseclient
url = "http://127.0.0.1:8000/events"
# you may set channel_id parameter to receive special events
# url = "http://127.0.0.1:8000/events?channel_id=foo"
response = requests.get(url, stream=True)
client = sseclient.SSEClient(response)
for event in client.events():
print(event.id)
print(event.event)
print(event.retry)
pprint.pprint(json.loads(event.data))
Requirements
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
sanic_sse-0.3.1.tar.gz
(5.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sanic_sse-0.3.1.tar.gz.
File metadata
- Download URL: sanic_sse-0.3.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa74e12b400abdb17570dcaffbdfae1d2e572560705e2896179f2d243e429e51
|
|
| MD5 |
52b67ee050b2e318580a3c38cb0f8809
|
|
| BLAKE2b-256 |
9e05592f92973b1f1c82d4ba42c18954d200f4f705b65bf3e76dfc2a4a4efa23
|
File details
Details for the file sanic_sse-0.3.1-py3-none-any.whl.
File metadata
- Download URL: sanic_sse-0.3.1-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.4.0-1031-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa219853759334fa753eab73724c4973916b6d4279a80770367055929edb88c6
|
|
| MD5 |
350e6d9f90d2d0bdbdf5cd67ce218dad
|
|
| BLAKE2b-256 |
f993ca9584948538ff4125124052e92be3bb07194b2d2af9c3d09168a1a7dd72
|