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')


app = web.Application()
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')


app = web.Application()
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-1.0.0.tar.gz (8.4 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-1.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aiohttp-sse-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1a1b9c5683050b40a1aefa543f65fa32eae44312fb6bdf77f039e1d1ab512a82
MD5 c077380ea454707772bcfe55af6896af
BLAKE2b-256 1921fed42fb3a8066930c94060f802a30fefc83499737f92a4a84a7a4adb8e93

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiohttp_sse-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87fbcef0493e992f0acb85c650bf82421340493a7fa6ac01dcf073e0ce875824
MD5 7c12fce173c531dc44e78a6ebed8d627
BLAKE2b-256 6fcc3d484055a00087ade90452b4ef24f1dd899686cbbe5103fe442142d42d97

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