sse events sample
This commit is contained in:
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user