1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
import asyncio
import itertools
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from sse_starlette.sse import EventSourceResponse
"""
https://github.com/sysid/sse-starlette/issues/132
# Run uvicorn
$ uvicorn issue132:app
# Open the app in a browser
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: 127.0.0.1:49184 - "GET /events HTTP/1.1" 200 OK
# Press CTRL+C to stop the server
^CINFO: Shutting down
INFO: Waiting for connections to close. (CTRL+C to force quit)
"""
app = FastAPI()
@app.get("/")
async def index() -> HTMLResponse:
return HTMLResponse(
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SSE ASGI tests</title>
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
<script src="https://unpkg.com/htmx-ext-sse@2.2.2"></script>
<link rel="stylesheet" href="https://cdn.simplecss.org/simple.min.css">
</head>
<body>
<a hx-ext="sse" sse-connect="/events" sse-swap="message">0</a>
</body>
</html>
"""
)
@app.get("/events")
async def event_source() -> EventSourceResponse:
async def event_generator():
for x in itertools.count():
yield x
await asyncio.sleep(1)
return EventSourceResponse(event_generator(), media_type="text/event-stream")
if __name__ == "__main__":
import uvicorn
uvicorn.main()
# uvicorn.run(app, host="0.0.0.0", port=8000, log_level="info")
|