Skip to main content

Server-sent events support for aiohttp.

Project description

https://travis-ci.org/jettify/aiohttp_sse.svg?branch=master https://coveralls.io/repos/jettify/aiohttp_sse/badge.svg

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.

Example

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


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

    resp.stop_streaming()
    return resp


@asyncio.coroutine
def index(request):
    d = b"""
        <html>
        <head>
            <script type="text/javascript"
                src="http://code.jquery.com/jquery.min.js"></script>
            <script type="text/javascript">
            var evtSource = new EventSource("/hello");
            evtSource.onmessage = function(e) {
             $('#response').html(e.data);
            }

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


@asyncio.coroutine
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, '127.0.0.1', 8080)
    print("Server started at http://127.0.0.1:8080")
    return srv, handler


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

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiohttp-sse-0.0.2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for aiohttp-sse-0.0.2.tar.gz
Algorithm Hash digest
SHA256 af1b810157d3a479fde53fa9b7055f799602de2cabd28a4a6404393d379bc5be
MD5 4a1e53690264ed0505cd5e82cb57e0a2
BLAKE2b-256 aca4ee6137b08d36a0cfee47d59f177be078a722717b5f1f0fa4a83d1678f29e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp_sse-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 840be72a9be1aa20694aecd7b20328ec386a3e1a3da170ce4cf288810d372608
MD5 4161d485710df9c378dcf4fca2c145dc
BLAKE2b-256 051a22045a5f550230d9308661e9b14699ed03b3cf434956b6474acd050c4fb7

See more details on using hashes here.

Supported by

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