requests-sse is a Python client library for Server-Sent Events (SSE), built on top of the popular 'requests' HTTP library. It provides an `EventSource` object for consuming event streams, handling reconnections and message parsing according to the WHATWG SSE specification. The current stable version is 0.5.3, with a beta 0.6.0b0 indicating an upcoming major release. The library generally follows an active release cadence with minor updates and bug fixes.
pip install requests-sseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to connect to a Server-Sent Events stream using `EventSource` and iterate over incoming `MessageEvent` objects. It uses the `with` statement for proper resource management and shows how to access event properties like `id`, `event` (type), and `data`.
Review the 0.6.0b0 changelog carefully before upgrading. Ensure your code expects `event.data` and `event.type` to be strings. Adjust parsing logic if you were relying on previous whitespace handling or `retry` field parsing behavior.
Upgrade to Python 3.8 or a newer supported version (currently Python 3.8 to 3.11 for stable 0.5.3).
Remove the `option` parameter from `EventSource` constructor calls. If specific functionality was handled by `option`, you may need to find alternative ways to configure it or handle it externally.
When not using `EventSource` as a context manager (`with EventSource(...)`), ensure you explicitly call `es.close()` to properly release resources and close the HTTP session.
Remove the `option` parameter from your `EventSource` initialization. Review your code to adapt to this breaking change.
Ensure the server correctly sets the `Content-Type` header to `text/event-stream`. If on an older version of `requests-sse` (<0.5.3), upgrading might fix issues with extra `charset` parameters in the content-type.
If you have upgraded to `0.6.0b0` (or a future stable `0.6.0`), ensure your code always expects `event.data` and `event.type` to be strings and handles conversion (e.g., `json.loads(event.data)`) explicitly if needed.