Skip to main content

Server-sent events support for aiohttp.

Project description

https://github.com/aio-libs/aiohttp-sse/workflows/CI/badge.svg?event=push https://codecov.io/gh/aio-libs/aiohttp-sse/branch/master/graph/badge.svg Updates Chat on Gitter

The EventSource interface is used to receive server-sent events. It connects to a server over HTTP and receives events in text/event-stream format without closing the connection. aiohttp-sse provides support for server-sent events for aiohttp.

Installation

Installation process as simple as:

$ pip install aiohttp-sse

Example

import asyncio
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:
        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.StreamResponse:
    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)
web.run_app(app, host="127.0.0.1", port=8080)

EventSource Protocol

Requirements

License

The aiohttp-sse is offered under Apache 2.0 license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp-sse-2.2.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

aiohttp_sse-2.2.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-sse-2.2.0.tar.gz.

File metadata

  • Download URL: aiohttp-sse-2.2.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for aiohttp-sse-2.2.0.tar.gz
Algorithm Hash digest
SHA256 a48dd5774031d3f41a29e159ebdbb93e89c8f37c1e9e83e196296be51885a5c2
MD5 8c86e737a2f3642941c1d383f42ce6ae
BLAKE2b-256 80df4ddb30e689695fd91cf41c072e154061120ed166e8baf6c9a0020f27dffc

See more details on using hashes here.

File details

Details for the file aiohttp_sse-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: aiohttp_sse-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for aiohttp_sse-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 339f9803bcf4682a2060e75548760d86abe4424a0d92ba66ff4985de3bd743dc
MD5 a68b27baacf7606fc5abbb266c956f6d
BLAKE2b-256 5ab8bf448b1d2dbe6cf16c3be0b92230a8f032f2f0ed159a2299284c709819c8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page