Skip to main content

Server-sent events support for aiohttp.

Project description

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.


import asyncio
from aiohttp.web import Application, Response
from aiohttp_sse import EventSourceResponse

def hello(request):
    resp = EventSourceResponse()
    for i in range(0, 100):
        yield from asyncio.sleep(1, loop=loop)
        resp.send('foo {}'.format(i))

    return resp

def index(request):
    d = b"""
            <script type="text/javascript"
            <script type="text/javascript">
            var evtSource = new EventSource("/hello");
            evtSource.onmessage = function(e) {

            <h1>Response from server:</h1>
            <div id="response"></div>
    return Response(body=d)

def init(loop):
    app = Application(loop=loop)
    app.router.add_route('GET', '/hello', hello)
    app.router.add_route('GET', '/index', index)

    handler = app.make_handler()
    srv = yield from loop.create_server(handler, '', 8080)
    print("Server started at")
    return srv, handler

loop = asyncio.get_event_loop()
srv, handler = loop.run_until_complete(init(loop))
except KeyboardInterrupt:

EventSource Protocol



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-0.0.2.tar.gz (8.5 kB view hashes)

Uploaded Source

Built Distribution

aiohttp_sse-0.0.2-py3-none-any.whl (4.4 kB view hashes)

Uploaded Python 3

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