{"id":8818,"library":"aiohttp-sse-client","title":"aiohttp-sse-client","description":"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.","status":"maintenance","version":"0.2.1","language":"en","source_language":"en","source_url":"https://github.com/rtfol/aiohttp-sse-client","tags":["asyncio","aiohttp","sse","client","server-sent events"],"install":[{"cmd":"pip install aiohttp-sse-client","lang":"bash","label":"Install stable release"}],"dependencies":[{"reason":"Core asynchronous HTTP client framework.","package":"aiohttp","optional":false}],"imports":[{"note":"The primary class for connecting to and consuming an SSE stream. It's typically imported with an alias for brevity.","symbol":"EventSource","correct":"from aiohttp_sse_client import client as sse_client # then use sse_client.EventSource"}],"quickstart":{"code":"import asyncio\nimport aiohttp\nfrom aiohttp_sse_client import client as sse_client\n\nasync def listen_to_sse(url):\n    session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None))\n    try:\n        async with sse_client.EventSource(url, session=session) as event_source:\n            print(f\"Connected to SSE stream at {url}\")\n            async for event in event_source:\n                print(f\"Event ID: {event.id}, Type: {event.type}, Data: {event.data}\")\n    except aiohttp.ClientConnectorError as e:\n        print(f\"Connection error: {e}\")\n    except ConnectionError as e:\n        print(f\"Stream disconnected: {e}\")\n    except asyncio.TimeoutError:\n        print(\"SSE stream timed out. Ensure server sends keep-alives or configure ClientTimeout.\")\n    finally:\n        await session.close()\n\nasync def main():\n    # Example public SSE stream (e.g., Wikimedia recent changes)\n    sse_url = \"https://stream.wikimedia.org/v2/stream/recentchange\"\n    await listen_to_sse(sse_url)\n\nif __name__ == '__main__':\n    asyncio.run(main())\n","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your environment uses Python 3.6 or newer. For Python 3.7+ support, you might consider the `aiohttp-sse-client2` fork.","message":"Version 0.2.0 dropped official support for Python 3.5. Subsequent versions (including 0.2.1) require Python 3.6 or newer.","severity":"breaking","affected_versions":"0.2.0 and later"},{"fix":"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)`.","message":"The underlying `aiohttp.ClientSession` used by `EventSource` has a default total timeout (typically 5 minutes). For long-lived SSE connections, this can lead to `asyncio.TimeoutError` or `ClientOSError: [Errno 104] Connection reset by peer` even with server-side keep-alives.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Asynchronous network operations are inherently prone to disconnections. Client applications must implement robust error handling and reconnection logic to gracefully manage these failures.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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)`).","cause":"The default `aiohttp.ClientSession` timeout (usually 5 minutes) expires on a long-lived SSE connection that appears idle to the client, even if the server is sending occasional keep-alives.","error":"asyncio.TimeoutError: Operation timed out"},{"fix":"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.","cause":"The server or an intermediary closed the connection unexpectedly, or the client-side timeout in `aiohttp.ClientSession` was reached, causing the connection to be forcefully closed.","error":"ClientOSError: [Errno 104] Connection reset by peer"}]}