aiohttp-sse-client2 is a Python client library for Server-Sent Events (SSE) built on top of aiohttp, providing a straightforward interface for consuming event streams. It is a fork of the original `aiohttp-sse-client` project, featuring improved error messages and updated compatibility with newer Python versions. The current version is 0.3.0, released in February 2023, indicating an infrequent release cadence.
pip install aiohttp-sse-client2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates connecting to a public SSE stream and iterating over incoming events. It also includes basic error handling for common connection issues. For long-running connections, consider configuring `aiohttp.ClientTimeout` explicitly.
Ensure your environment uses Python 3.7 or newer for version 0.3.0, or Python 3.6 or newer for 0.2.x releases.
When initializing `EventSource`, pass a custom `aiohttp.ClientSession` with an explicitly configured timeout, typically `aiohttp.ClientTimeout(total=None)` for indefinite connections, or a sufficiently large value. Example: `session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None))` then `sse_client.EventSource(url, session=session)`.
Be aware of the project's fork status. Monitor the GitHub repository for updates regarding its maintenance and consider the implications for long-term project stability if the original `aiohttp-sse-client` project becomes active again.
Implement comprehensive `try...except` blocks around the event iteration to gracefully handle `ConnectionError`, `asyncio.TimeoutError`, and other network-related exceptions. Consider adding retry mechanisms with exponential backoff for resilience.
Install the package using pip: 'pip install aiohttp-sse-client2'.
Ensure the correct import statement: 'from aiohttp_sse_client2 import client as sse_client'.
Verify the library's documentation for the correct usage and available classes.
Check the connection to the event source and ensure it returns a valid iterable object.
Ensure the event loop remains open until all tasks are finished, or restart the event loop if necessary.