{"id":6505,"library":"aiohttp-sse","title":"aiohttp-sse","description":"aiohttp-sse provides server-sent events support for aiohttp. It allows building real-time web applications where the server pushes updates to clients over a single HTTP connection. The current version is 2.2.0, and it has a relatively active release cadence with several updates per year, focusing on Python and aiohttp compatibility.","status":"active","version":"2.2.0","language":"en","source_language":"en","source_url":"https://github.com/aio-libs/aiohttp_sse/","tags":["aiohttp","sse","server-sent-events","asyncio","real-time"],"install":[{"cmd":"pip install aiohttp-sse","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for web server functionality. Requires aiohttp 3+ for current versions.","package":"aiohttp","optional":false},{"reason":"Requires Python 3.8+ as of version 2.2.0.","package":"python","optional":false}],"imports":[{"symbol":"sse_response","correct":"from aiohttp_sse import sse_response"},{"symbol":"EventSourceResponse","correct":"from aiohttp_sse import EventSourceResponse"}],"quickstart":{"code":"import asyncio\nimport json\nfrom datetime import datetime\nfrom aiohttp import web\nfrom aiohttp_sse import sse_response\n\nasync def hello(request: web.Request) -> web.StreamResponse:\n    async with sse_response(request) as resp:\n        # Optional: send an initial comment to stop browser loading spinner\n        await resp.send(None, event='comment', data='connected')\n        while resp.is_connected():\n            time_dict = {\"time\": f\"Server Time : {datetime.now()}\"}\n            data = json.dumps(time_dict, indent=2)\n            print(data)\n            await resp.send(data)\n            await asyncio.sleep(1)\n    return resp\n\nasync def index(_request: web.Request) -> web.Response:\n    html = \"\"\"\n<html>\n<body>\n    <script>\n        var eventSource = new EventSource(\"/hello\");\n        eventSource.addEventListener(\"message\", event => {\n            document.getElementById(\"response\").innerText = event.data;\n        });\n    </script>\n    <h1>Response from server:</h1>\n    <div id=\"response\"></div>\n</body>\n</html>\n\"\"\"\n    return web.Response(text=html, content_type=\"text/html\")\n\napp = web.Application()\napp.router.add_route(\"GET\", \"/hello\", hello)\napp.router.add_route(\"GET\", \"/\", index)\n\nif __name__ == '__main__':\n    web.run_app(app, host=\"127.0.0.1\", port=8080)","lang":"python","description":"This quickstart sets up an aiohttp web server that serves an HTML page. The HTML page contains JavaScript that connects to a '/hello' endpoint via Server-Sent Events. The '/hello' endpoint uses `aiohttp-sse`'s `sse_response` context manager to continuously send the current server time to the connected client."},"warnings":[{"fix":"Upgrade Python to 3.8 or newer, or pin to an older `aiohttp-sse` version compatible with your Python environment.","message":"Python version support has been frequently dropped in major and minor releases. Version 2.2.0 dropped Python 3.7, and 2.1.0 dropped Python 3.6 support. Ensure your Python environment meets the `>=3.8` requirement for current versions.","severity":"breaking","affected_versions":"<2.2.0"},{"fix":"Check the `aiohttp-sse` release notes for the exact `aiohttp` version compatibility. Upgrade both `aiohttp-sse` and `aiohttp` to compatible versions.","message":"Major versions of `aiohttp-sse` are tied to specific `aiohttp` versions. Version 2.0.0 introduced compatibility with `aiohttp 3.0+`, while older versions like 1.0.0 required `aiohttp2+`, and 0.1.0 was for `aiohttp<2.0`. Mismatched `aiohttp` versions can lead to runtime errors.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Send an initial 'comment' event (e.g., `await resp.send(None, event='comment', data='connected')`) after establishing the SSE connection. This signals the browser that the connection is live and usually stops the spinner.","message":"Browsers keep the tab's loading spinner active as long as an SSE connection remains open, which can be confusing for users.","severity":"gotcha","affected_versions":"All"},{"fix":"Upgrade to `aiohttp-sse` version 2.2.0 or newer to benefit from the fix for `EventSourceResponse.wait()` cancellation behavior on Python 3.11+.","message":"Prior to v2.2.0, specifically on Python 3.11+, the `EventSourceResponse.wait()` method could swallow user cancellation, making it difficult to gracefully shut down SSE streams.","severity":"gotcha","affected_versions":"<2.2.0"},{"fix":"For client-side SSE consumption, consider using dedicated libraries like `aiohttp-sse-client` or `aiosseclient` built on top of `aiohttp`, or implement custom parsing logic.","message":"`aiohttp` itself does not natively provide client-side Server-Sent Events parsing. Users attempting to consume `aiohttp-sse` streams with a plain `aiohttp.ClientSession` might find parsing challenging.","severity":"gotcha","affected_versions":"All"},{"fix":"Design client-side applications to manage and reuse SSE connections efficiently. Avoid opening an excessive number of simultaneous SSE streams from the same client to the same server. Adjusting browser settings (e.g., `network.http.speculative-parallel-limit` in Firefox) can also be done for testing, but is not a general solution for users.","message":"Web browsers often impose a limit (e.g., 6) on the number of concurrent HTTP connections to a single domain. Exceeding this limit with multiple SSE connections can cause new requests to hang or time out.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"Install the package using pip: 'pip install aiohttp-sse'.","cause":"The 'aiohttp-sse' package is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'aiohttp_sse'"},{"fix":"Use 'sse_response' instead: 'from aiohttp_sse import sse_response'.","cause":"The 'EventSourceResponse' class is not available in the 'aiohttp_sse' module.","error":"ImportError: cannot import name 'EventSourceResponse' from 'aiohttp_sse'"},{"fix":"Use 'sse_response' which provides the 'send' method: 'from aiohttp_sse import sse_response'.","cause":"The 'EventSourceResponse' object does not have a 'send' method.","error":"AttributeError: 'EventSourceResponse' object has no attribute 'send'"},{"fix":"Install the library using pip: `pip install aiohttp-sse`","cause":"The `aiohttp-sse` library is not installed in your Python environment or is not accessible on the Python path.","error":"ModuleNotFoundError: No module named 'aiohttp-sse'"},{"fix":"Rename any local file named `aiohttp.py` to something else (e.g., `my_aiohttp_app.py`). Ensure you are using `aiohttp.ClientSession()` for client requests, as direct `aiohttp.get()` was deprecated.","cause":"This typically occurs when you have a file named `aiohttp.py` in your project directory, which shadows the actual `aiohttp` library, or if you are trying to use an outdated API for `aiohttp` client requests.","error":"AttributeError: module 'aiohttp' has no attribute 'ClientSession'"}]}