I am trying to broadcast SSE to all clients once the user makes a request.
I was able to get this basic example up and running
async def dashboard_oc_interface(request):
"""
Sends server-sent events to the client.
"""
async def event_stream():
i = 0
while True:
# yield {'id' : random.randint(0, 99) , 'status' : 'success'}
yield f'data: {random.choice(range(1,40))},SUCCESSnn'
await asyncio.sleep(100)
return StreamingHttpResponse(event_stream(), content_type='text/event-stream')
But I am unsure how to do it below:
# user will be making this request
def post_request(request):
success = random.choice([True, False]) # NOT ACTUAL LOGIC
return JsonResponse ({'success' : success })
# I would like to stream the success data from above
async def stream():
# HOW CAN I STREAM ONLY WHEN post request
# we will listen here
async def event_stream(request):
return StreamingHttpResponse(stream(), content_type='text/event-stream')
async def dashboard_oc_interface(request):
"""
Sends server-sent events to the client.
"""
async def event_stream():
i = 0
while True:
# yield {'id' : random.randint(0, 99) , 'status' : 'success'}
yield f'data: {random.choice(range(1,40))},SUCCESSnn'
await asyncio.sleep(100)
return StreamingHttpResponse(event_stream(), content_type='text/event-stream')