mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 06:46:14 +00:00
a3b49ab9fd
New documentation pages: - Authentication: API keys, JWT tokens, session auth, custom exceptions - WebSocket Tutorial: echo server, chat room, HTML client, data formats - Writing Middleware: hooks vs middleware, Starlette integration, ordering - Configuration: env vars, .env files, secret keys, debug mode, production setup Also: - Complete working example at end of quickstart with cross-references - Three new example files: rest_api.py, websocket_chat.py, sse_stream.py - Changelog updated for v3.2.0 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
43 lines
900 B
Python
43 lines
900 B
Python
# Server-Sent Events streaming example.
|
|
# https://responder.kennethreitz.org/tour.html#server-sent-events-sse
|
|
import asyncio
|
|
|
|
import responder
|
|
|
|
api = responder.API()
|
|
|
|
|
|
@api.route("/")
|
|
def index(req, resp):
|
|
resp.html = """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<body>
|
|
<h1>SSE Stream</h1>
|
|
<div id="events"></div>
|
|
<script>
|
|
const source = new EventSource("/stream");
|
|
const events = document.getElementById("events");
|
|
source.onmessage = (e) => {
|
|
const p = document.createElement("p");
|
|
p.textContent = e.data;
|
|
events.appendChild(p);
|
|
};
|
|
</script>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
@api.route("/stream")
|
|
async def stream(req, resp):
|
|
@resp.sse
|
|
async def events():
|
|
for i in range(20):
|
|
yield {"data": f"Event #{i}"}
|
|
await asyncio.sleep(0.5)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
api.run()
|