Skip to main content

fastapi-sse-wrapper

A small, professional SSE (Server-Sent Events) wrapper for FastAPI, designed to help developers build streaming AI agent and chatbot backends.


✨ Features

  • Named channels with multiple client connections
  • Broadcast and targeted messages
  • Message history & replay for reconnection support
  • Keepalive pings and recommended HTTP headers
  • Authentication hooks (pluggable)
  • Minimal dependencies (FastAPI + Uvicorn)
  • Typed public API and examples

📦 Installation

pip install fastapi-sse-wrapper

🚀 Quick Start

Create SSE Manager and Endpoint

from fastapi import FastAPI
from fastapi_sse.core import SSEManager
from fastapi_sse.events import SSEEvent

app = FastAPI()
manager = SSEManager(app=app)

# Register SSE endpoint for channel "chat"
app.add_api_route("/sse/chat/{client_id}", manager.endpoint("chat"), methods=["GET"])

Send Events

@app.post("/send/{channel}")
async def send_message(channel: str, payload: dict):
    ch = manager.channel(channel)
    ev = SSEEvent(data=payload, event="message")
    await ch.send(ev)
    return {"status": "ok", "id": ev.id}

Connect Client

Browser client using EventSource:

const es = new EventSource("http://127.0.0.1:8000/sse/chat/myclient");
es.onmessage = e => console.log("msg", e.data);
es.addEventListener("message", e => console.log("event:message", e.data));

🧠 Example: Streaming AI Agent Tokens

@app.post("/agent/send/{conversation_id}")
async def agent_send(conversation_id: str, payload: dict):
    ch = manager.channel("agent")
    start = SSEEvent(data={"cid": conversation_id, "status": "started"}, event="agent.start")
    await ch.send(start)

    for token in ["Hello", ",", " this", " is", " streamed"]:
        await asyncio.sleep(0.5)
        ev = SSEEvent(data={"cid": conversation_id, "token": token}, event="agent.token")
        await ch.send(ev)

    done = SSEEvent(data={"cid": conversation_id, "status": "done"}, event="agent.done")
    await ch.send(done)
    return {"started": True}

🔒 Authentication

Use an auth hook:

from fastapi_sse.middleware import token_auth_hook_factory

def token_lookup(token: str):
    return "user1" if token == "secret" else None

manager = SSEManager(app, auth_hook=token_auth_hook_factory(token_lookup))

📂 Project Structure

fastapi-sse-wrapper/
├─ fastapi_sse/
│  ├─ core.py         # Manager + connections
│  ├─ events.py       # SSEEvent model
│  ├─ middleware.py   # Auth hook helpers
│  ├─ backends.py     # In-memory history backend
│  └─ utils.py
├─ examples/
│  ├─ simple_app.py
│  └─ agent_integration.py
├─ tests/
└─ README.md

⚡ Roadmap

  • Redis backend for multi-worker support
  • Advanced reconnection + replay strategies
  • Rate limiting and access control
  • TypeScript/JS client SDK

📜 License

MIT License.

Metadata

Release files for fastapi-sse-wrapper 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-sse-wrapper 0.1.0
File Size Uploaded Provenance Actions
fastapi_sse_wrapper-0.1.0.tar.gz 18.7 kB Without provenance Details for fastapi_sse_wrapper-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-sse-wrapper 0.1.0
File Interpreter ABI Platform Actions
fastapi_sse_wrapper-0.1.0-py3-none-any.whl Python 3 none any Details for fastapi_sse_wrapper-0.1.0-py3-none-any.whl

Total release size: 38.2 kB

Release files / fastapi_sse_wrapper-0.1.0.tar.gz

Download URL fastapi_sse_wrapper-0.1.0.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
823c8034bf918acc046adcf85ba3c5a5af838ae4d288459e279c3654ce61c5e8
BLAKE2b-256 checksum
How to use checksums
d67822305bdc4b4193177be3b00bea1e43d8aaade804d5045d0513755565a2d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.2

Release files / fastapi_sse_wrapper-0.1.0-py3-none-any.whl

Download URL fastapi_sse_wrapper-0.1.0-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f1fa66e1e8804bee26b594810af307340b260a6b43c0dddc0f758f67b6a2f359
BLAKE2b-256 checksum
How to use checksums
64b8845af641698848fe7e8d5806df0235df595ad58d8df75c2c3de1589ae893
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.2

Security for fastapi-sse-wrapper 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page