Skip to main content

A tiny library for sending Server-Sent Events (SSE) in FastAPI

Project description

FastAPI-SSE

PyPI - Downloads PyPI - Version Rye

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 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

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

fastapi_sse-1.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

fastapi_sse-1.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_sse-1.0.1.tar.gz.

File metadata

  • Download URL: fastapi_sse-1.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for fastapi_sse-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4b2c0c513bb292fc8b4e96e6b689fb1d2985a845d2fca4f772caba754aa543ab
MD5 99940fb285f576b050e1c1733ebed2f5
BLAKE2b-256 8c6d2f4d2ca3501a7be10a1b69e47e58bc0f4ab7e3edb5cc796c276b3e3cc18a

See more details on using hashes here.

File details

Details for the file fastapi_sse-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: fastapi_sse-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for fastapi_sse-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 570a51a1c7b4fddcf1797852c7acf889dccdce5e9433dd135b9da748fb1b62b1
MD5 367ada825edab23d81c64a58f203f923
BLAKE2b-256 762e5df5baabf8cf8ef50cbddb57e9834e29946c59b7e156612e77d15b862d28

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page