Skip to content

rutsky/aiohttp-sse

 
 

Repository files navigation

aiohttp-sse

image

image

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_sse import sse_response


async def hello(request):
    loop = asyncio.get_event_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 = 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)


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.web import Application, Response
from aiohttp_sse import sse_response


async def hello(request):
    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 = 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)


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.

About

Server-sent events support for aiohttp

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 95.5%
  • Makefile 4.5%