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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file aiohttp-sse-0.1.0.tar.gz
.
File metadata
- Download URL: aiohttp-sse-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
119ad574a37efe05b0fb2bfb580195314e3ac821d82c31c7f4235ca58b5f25f2
|
|
MD5 |
bfada5db04047fc5251809e48cc54dfc
|
|
BLAKE2b-256 |
c44eb950284b6b4b2b7531b0c28b9f9c86f4e46eabd471e198b269b1b9770a43
|
File details
Details for the file aiohttp_sse-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_sse-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
1904c410fe4761ad2638a964730605ac443bb5c94ebf2d92c540db44f49affbc
|
|
MD5 |
b9ca42d6669b76c30cbd94dde87a863d
|
|
BLAKE2b-256 |
6fb0d99b66dac1a18e46244c7ae6ce8e9a8354aa6b1d651dd262ab623072f8ff
|