FastAPI-SSE
A tiny library for sending Server-Sent Events (SSE) in FastAPI
Server-Sent Events (SSE) provide a way to stream real-time updates from the server to the client over HTTP. This library allows sending Pydantic models as SSE events in FastAPI, formatted as JSON.
from fastapi import FastAPI
from fastapi_sse import sse_response
from pydantic import BaseModel
app = FastAPI()
class MyMessage(BaseModel):
text: str
@app.get("/stream")
@sse_handler()
async def message_generator(some_url_arg: str):
yield MyMessage(text=f"Hello, {some_url_arg}!")
yield MyMessage(text="Another message")
And on the frontend to handle:
const eventSource = new EventSource('/stream');
eventSource.onmessage = (event) => {
const message = JSON.parse(event.data);
console.log('Received message:', message);
};
Installation
Install the library using pip:
pip install fastapi-sse
Typed Events
You can also use typed_sse_response to send messages with the event type populated with the model name.
Note: When emitting typed messages, EventSource will no longer trigger the onmessage event. Instead, you must attach a handler via .addEventListener('<eventType>', ...).:
const eventSource = new EventSource('/stream');
eventSource.addEventListener('MyMessage', (event) => {
const message = JSON.parse(event.data);
console.log('Received message:', message);
});
Development
FastAPI-SSE uses Rye for dependency management and the development workflow. To get started with development, ensure you have Rye installed and then clone the repository and set up the environment:
git clone https://github.com/MatthewScholefield/fastapi-sse.git
cd fastapi-sse
rye sync
rye run pre-commit install
# Run tests
rye test
Metadata
Release files for fastapi-sse 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_sse-1.1.1.tar.gz | 5.2 kB | Without provenance | Details for fastapi_sse-1.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_sse-1.1.1-py3-none-any.whl | Python 3 | none | any | Details for fastapi_sse-1.1.1-py3-none-any.whl |
Total release size: 9.2 kB
Release files / fastapi_sse-1.1.1.tar.gz
| Download URL | fastapi_sse-1.1.1.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d9da5a49cd5a84f0d976f7d593b1e08d6c16012881a1145a7b01e49527e6e355
|
|
BLAKE2b-256 checksum How to use checksums |
939cca9e6813ccad11f2687742c1f261b72cf6851407921055451180d6f0ee74
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.4
|
Release files / fastapi_sse-1.1.1-py3-none-any.whl
| Download URL | fastapi_sse-1.1.1-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b006a33001f076fccf67957e65b0975c31b91c77cc6c13ee161a9b6c5412d330
|
|
BLAKE2b-256 checksum How to use checksums |
3093e8aa8d58ae72cab25881e248264c23ceea7ef91f9b45bcafd50a0641ed6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.4
|
Security for fastapi-sse 1.1.1
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.