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

@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

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.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

fastapi_sse-1.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_sse-1.1.0.tar.gz
  • Upload date:
  • Size: 5.1 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.1.0.tar.gz
Algorithm Hash digest
SHA256 438d0305751df171deb173217df636d1d18aa14d961c772c74245a27f995b153
MD5 134834531c7467e169ce7c97a624fc88
BLAKE2b-256 4833ac04e367ce55f26673b3ac66ebe7ba658ee33b7ef60c1e96d59b32aca8b9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_sse-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a9ad1dfb989e038384f551633753cee318a4662b9d258e7bf9bce0142dbc8a4
MD5 33374c5aeedd4c66ba8dc3f5bf96be58
BLAKE2b-256 17d5181a66b8445e5232353e7d4e708711495351884864ff0a1731961d059e6b

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