aiohttp-sse-client is a Python client library for consuming Server-Sent Events (SSE) streams, built on top of `aiohttp`. It provides a simple asynchronous interface for connecting to SSE sources, processing incoming events, and handling automatic reconnections. The current version is 0.2.1, released in February 2021, indicating an infrequent release cadence. Note that a fork, `aiohttp-sse-client2`, exists to address maintenance and support newer Python versions.
pip install aiohttp-sse-clientVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates connecting to a public SSE stream and iterating over incoming events. It includes robust error handling for common connection issues and configures `aiohttp.ClientTimeout` for long-running connections to prevent premature timeouts.
Ensure your environment uses Python 3.6 or newer. For Python 3.7+ support, you might consider the `aiohttp-sse-client2` fork.
When initializing `EventSource`, pass a custom `aiohttp.ClientSession` with an explicitly configured timeout. For indefinite connections, use `aiohttp.ClientTimeout(total=None)`, or a sufficiently large numeric value. Example: `session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None)); sse_client.EventSource(url, session=session)`.
Implement comprehensive `try...except` blocks around the event iteration to handle `ConnectionError`, `aiohttp.ClientConnectorError`, `asyncio.TimeoutError`, and other network-related exceptions. Consider adding retry mechanisms with exponential backoff if a simple `pass` is insufficient for your use case.
Pass a `aiohttp.ClientSession` with a `aiohttp.ClientTimeout(total=None)` or a very large `total` value to `EventSource` to disable or extend the timeout. (e.g., `session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None)); EventSource(url, session=session)`).
Implement robust `try...except ConnectionError` or specific `aiohttp.ClientConnectorError` handling. For long-running streams, also configure `aiohttp.ClientTimeout(total=None)` to prevent client-side timeouts from causing resets.