Skip to main content

Server-sent events support for aiohttp.

Project description

https://travis-ci.org/aio-libs/aiohttp-sse.svg?branch=master https://codecov.io/gh/aio-libs/aiohttp-sse/branch/master/graph/badge.svg Updates

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

Mailing List

aio-libs google group: https://groups.google.com/forum/#!forum/aio-libs

Example

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


async def hello(request):
    loop = request.app.loop
    resp = await sse_response(request)
    for i in range(0, 100):
        print('foo')
        await asyncio.sleep(1, loop=loop)
        resp.send('foo {}'.format(i))

    resp.stop_streaming()
    return resp


async def index(request):
    d = """
        <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(text=d, content_type='text/html')


loop = asyncio.get_event_loop()
app = web.Application(loop=loop)
app.router.add_route('GET', '/hello', hello)
app.router.add_route('GET', '/index', index)
web.run_app(app, host='127.0.0.1', port=8080)

Same example with asynchronous context manager interface (python3.5+)

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


async def hello(request):
    loop = request.app.loop
    resp = await sse_response(request)
    async with resp:
        for i in range(0, 100):
            print('foo')
            await asyncio.sleep(1, loop=loop)
            resp.send('foo {}'.format(i))
    return resp


async def index(request):
    d = """
        <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(text=d, content_type='text/html')


loop = asyncio.get_event_loop()
app = web.Application(loop=loop)
app.router.add_route('GET', '/hello', hello)
app.router.add_route('GET', '/index', 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-0.0.3a0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiohttp_sse-0.0.3a0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file aiohttp-sse-0.0.3a0.tar.gz.

File metadata

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

File hashes

Hashes for aiohttp-sse-0.0.3a0.tar.gz
Algorithm Hash digest
SHA256 eb253c7130a7f04ed5d1528fda5caac038d077b7c2f2d624d99fcf0a92820e0d
MD5 33606e59c2ed79dc9da588f71039a4d2
BLAKE2b-256 694155e19f2c1610370771c110a921e08ae149f13aab5902ebd727ffa6b46397

See more details on using hashes here.

File details

Details for the file aiohttp_sse-0.0.3a0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiohttp_sse-0.0.3a0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ddced94fcf26daadd4700f2233b30120f5cf2f4399e638fd6b37736c045f6bc
MD5 fdba3d218f6cd901c7803de20147b1f8
BLAKE2b-256 b93d7e8750a77cf764d9eed907f5981e4277b9c42dbe294f0a782110abfcfe19

See more details on using hashes here.

Supported by

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