Registry / http-networking / aiohttp-sse

aiohttp-sse

JSON →
library2.2.0pypypi✓ verified 85d ago

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.

pip install aiohttp-sse
INSTALL
IMPORT
SIG · AIOHTTP-SSE
A
aiohttp-sse
http-networkingpythonv2.2.0
Install
4.5s avg
Import
652ms
Disk
27MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.689s · 27.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 4.5s · import 0.616s · 29MB
27MB installed
● package 27MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sse_response
from aiohttp_sse import sse_response
EventSourceResponse
from aiohttp_sse import EventSourceResponse

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.

import asyncio import json from datetime import datetime from aiohttp import web from aiohttp_sse import sse_response async def hello(request: web.Request) -> web.StreamResponse: async with sse_response(request) as resp: # Optional: send an initial comment to stop browser loading spinner await resp.send(None, event='comment', data='connected') while resp.is_connected(): time_dict = {"time": f"Server Time : {datetime.now()}"} data = json.dumps(time_dict, indent=2) print(data) await resp.send(data) await asyncio.sleep(1) return resp async def index(_request: web.Request) -> web.Response: html = """ <html> <body> <script> var eventSource = new EventSource("/hello"); eventSource.addEventListener("message", event => { document.getElementById("response").innerText = event.data; }); </script> <h1>Response from server:</h1> <div id="response"></div> </body> </html> """ return web.Response(text=html, content_type="text/html") app = web.Application() app.router.add_route("GET", "/hello", hello) app.router.add_route("GET", "/", index) if __name__ == '__main__': web.run_app(app, host="127.0.0.1", port=8080)
Debug
Known issues
breakingPython 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.
fix
Upgrade Python to 3.8 or newer, or pin to an older `aiohttp-sse` version compatible with your Python environment.
affects: <2.2.0
breakingMajor 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.
fix
Check the `aiohttp-sse` release notes for the exact `aiohttp` version compatibility. Upgrade both `aiohttp-sse` and `aiohttp` to compatible versions.
affects: <2.0.0
gotchaBrowsers keep the tab's loading spinner active as long as an SSE connection remains open, which can be confusing for users.
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.
affects: All
gotchaPrior 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.
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+.
affects: <2.2.0
gotcha`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.
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.
affects: All
gotchaWeb 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.
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.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'aiohttp_sse'
The 'aiohttp-sse' package is not installed in the Python environment.
fix
Install the package using pip: 'pip install aiohttp-sse'.
ImportError: cannot import name 'EventSourceResponse' from 'aiohttp_sse'
The 'EventSourceResponse' class is not available in the 'aiohttp_sse' module.
fix
Use 'sse_response' instead: 'from aiohttp_sse import sse_response'.
AttributeError: 'EventSourceResponse' object has no attribute 'send'
The 'EventSourceResponse' object does not have a 'send' method.
fix
Use 'sse_response' which provides the 'send' method: 'from aiohttp_sse import sse_response'.
ModuleNotFoundError: No module named 'aiohttp-sse'
The `aiohttp-sse` library is not installed in your Python environment or is not accessible on the Python path.
fix
Install the library using pip: `pip install aiohttp-sse`
AttributeError: module 'aiohttp' has no attribute 'ClientSession'
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.
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.
Upgrade
Version history
2.2.0latest on PyPI · released Feb 29, 2024
Audit
Dependencies
aiohttprequiredCore dependency for web server functionality. Requires aiohttp 3+ for current versions.
pythonrequiredRequires Python 3.8+ as of version 2.2.0.
Agent activity
44 hits · last 30 days
node
38
OpenAI (training)
1
Resources
aiohttp-sse — pip install aiohttp-sse · libregistry