Skip to main content

"SSE plugin for Starlette"

Project description

Server Sent Events for Starlette and FastAPI

PyPI Version Build Status Code Coverage

Background: https://sysid.github.io/sse/

Installation:

pip install sse-starlette

Usage:

import asyncio
import uvicorn
from starlette.applications import Starlette
from starlette.routing import Route
from sse_starlette.sse import EventSourceResponse

async def numbers(minimum, maximum):
    for i in range(minimum, maximum + 1):
        await asyncio.sleep(0.9)
        yield dict(data=i)

async def sse(request):
    generator = numbers(1, 5)
    return EventSourceResponse(generator)

routes = [
    Route("/", endpoint=sse)
]

app = Starlette(debug=True, routes=routes)

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000, log_level='info')

Output:
output

Caveat: SSE streaming does not work in combination with GZipMiddleware.

Be aware that for proper server shutdown your application must stop all running tasks (generators). Otherwise you might experience the following warnings at shutdown: Waiting for background tasks to complete. (CTRL+C to force quit).

Client disconnects need to be handled in your Request handler (see example.py):

async def endless(req: Request):
    async def event_publisher():
        i = 0
        try:
          while True:
              i += 1
              yield dict(data=i)
              await asyncio.sleep(0.2)
        except asyncio.CancelledError as e:
          _log.info(f"Disconnected from client (via refresh/close) {req.client}")
          # Do any other cleanup, if any
          raise e
    return EventSourceResponse(event_publisher())

Run the tests:

make test

Changelog

1.0.0

  • drop support for python 3.6 and 3.7
  • removed unused private attribute _loop from class EventSourceResponse
  • updated example in README.md

0.10.3 (2022-01-25)

  • fix: use starlette's code to set proper content-type and charset
  • fix: update examples

0.10.0 (2021-12-14)

  • base EventSourceResponse on latest starlette StreamingResponse (0.17.1) and use anyio
  • breaking change: response.wait() and response.stop_streaming() removed

0.9.0 (2021-10-09)

  • add sse comment support

0.8.1 (2021-09-30)

  • minimum required python version relaxed to python 3.6

0.8.0 (2021-08-26)

  • using module-based logger instead of uvicorn logger

0.7.2 (2021-04-18)

  • refactoring: Github Actions introduced into build pipeline

0.6.2 (2020-12-19)

  • fix: correct shutdown signal handling in case of an endpoint which only yields sporadic messages

0.6.1 (2020-10-24)

  • updated example with proper error handling

0.6.0 (2020-10-24)

  • In case uvicorn is used: monkeypatch uvicorn signal-handler, in order to gracefully shutdown long-running handlers/generators.

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

sse-starlette-1.0.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sse_starlette-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file sse-starlette-1.0.0.tar.gz.

File metadata

  • Download URL: sse-starlette-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sse-starlette-1.0.0.tar.gz
Algorithm Hash digest
SHA256 14608559d40e3e7c6385e8c5a7b88468f7fc40c2277673a1fe8d26568e8d7c65
MD5 e1bd9cb063d53973239388055f1873eb
BLAKE2b-256 08271e78630cc0c97bcd2f3e053aac7433964ef261d4445c3a6e722be3437832

See more details on using hashes here.

File details

Details for the file sse_starlette-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sse_starlette-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for sse_starlette-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72438ed39b1612d1ea6d89a7c0af8afee6de0389dcbe2e77539001e78b5aa89c
MD5 48da134cc5345cf50a6299e87b2d273e
BLAKE2b-256 ed57ebcdc305df77eadbf7bbf5f4fedea5630f326105ebb0521acaceb238da5c

See more details on using hashes here.

Supported by

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