STREAM_DELAY = 1# second RETRY_TIMEOUT = 15000# milisecond
@app.get('/stream') asyncdefmessage_stream(request: Request): defnew_messages(): # Add logic here to check for new messages yield'Hello World' asyncdefevent_generator(): whileTrue: # If client closes connection, stop sending events ifawait request.is_disconnected(): break
# Checks for new messages and return them to client if any if new_messages(): yield { "event": "new_message", "id": "message_id", "retry": RETRY_TIMEOUT, "data": "message_content" }