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 | |
|---|---|---|---|
| fastapi_sse-1.1.1.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_sse-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
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
|