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

Uploaded Source

Built Distribution

fastapi_sse-1.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi_sse-1.1.1.tar.gz
  • Upload date:
  • Size: 5.2 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.1.tar.gz
Algorithm Hash digest
SHA256 d9da5a49cd5a84f0d976f7d593b1e08d6c16012881a1145a7b01e49527e6e355
MD5 4166b8d71aff716899264b7930352030
BLAKE2b-256 939cca9e6813ccad11f2687742c1f261b72cf6851407921055451180d6f0ee74

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fastapi_sse-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b006a33001f076fccf67957e65b0975c31b91c77cc6c13ee161a9b6c5412d330
MD5 3e14347a23c449396325eb1239780a7a
BLAKE2b-256 3093e8aa8d58ae72cab25881e248264c23ceea7ef91f9b45bcafd50a0641ed6e

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