sse events sample

This commit is contained in:
2026-06-18 01:21:40 +05:30
parent 69b795f9ca
commit 083fb6923d
4 changed files with 95 additions and 0 deletions

View File

@@ -11,6 +11,9 @@ in the OpenAPI specification.
"""
from fastapi import Response, HTTPException, UploadFile
from fastapi.responses import StreamingResponse
from sse import subscribe, unsubscribe
from models import (
ParentCreate,
@@ -363,3 +366,18 @@ def delete_appointment(id: int, response: Response):
except KeyError:
raise HTTPException(status_code=404, detail="Appointment not found")
response.status_code = 204
async def stream_calls():
"""Stream random animal sounds via SSE."""
q = await subscribe()
async def event_generator():
try:
while True:
data = await q.get()
yield f"data: {data}\n\n"
finally:
unsubscribe(q)
return StreamingResponse(event_generator(), media_type="text/event-stream")