A tiny library for sending Server-Sent Events (SSE) in FastAPI
Project description
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
async def message_generator():
yield MyMessage(text="Hello, SSE!")
yield MyMessage(text="Another message")
@app.get("/stream")
async def stream_messages():
return sse_response(message_generator())
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 git+https://github.com/MatthewScholefield/fastapi-sse.git
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](https://github.com/astral-sh/rye) installed and then clone the repository and set up the environment:
```sh
git clone https://github.com/MatthewScholefield/fastapi-sse.git
cd fastapi-sse
rye sync
rye run pre-commit install
# Run tests
rye test
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
Built Distribution
File details
Details for the file fastapi_sse-1.0.0.tar.gz
.
File metadata
- Download URL: fastapi_sse-1.0.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15c8e89dc597e9bbb240adcc181a0b930456cfd2539399dee84ec70eea5dfff9 |
|
MD5 | 50a6635824abedcb0c6ba0dbbee7a028 |
|
BLAKE2b-256 | 23ed2208f583cbab76a21ccdf568bcdba22af4829e88fbac7e23c07f96c91e12 |
File details
Details for the file fastapi_sse-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: fastapi_sse-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 54875f4d8fa549617b17073d0c35af614515749a7295b24687f1ee19c0530954 |
|
MD5 | af35b28b0e8bdfcb2f15e0a98e71b811 |
|
BLAKE2b-256 | fe5b18c9a84668fb47bd8299c9331eeb9deb85317c1aa1a5d39fd0f57510cd95 |