import asyncio
from django.http import StreamingHttpResponse
async def sse_stream(request):
async def event_stream():
# Example: send a keepalive ping every 15 seconds
while True:
yield f": keepalive \n\n"
await asyncio.sleep(15)
response = StreamingHttpResponse(event_stream(), content_type='text/event-stream')
response['Cache-Control'] = 'no-cache'
response['X-Accel-Buffering'] = 'no' # Disable Nginx buffering
return response