You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like a reference to the SSE stream, so I can send messages to the user from anywhere else in my app. I would like to avoid using queues, polling or pubsub, as they seem a lot more complicated than storing a reference.
For example, when a user makes a request that returns an SSE stream, I would like to store that SSE stream somewhere (service.add_subscriber(sse))
This code won't work because EventSourceResponse doesn't have a function to publish to the client. There is stream_response, but that seems to be about starting the stream, not just adding arbitrary messages.
@router.get("/stream")asyncdefget_sensor_data_sse(request: Request, service: SensorDataService=Depends(SensorDataProvider.get)) ->EventSourceResponse:
asyncdefremove_when_disconnected():
# remove when its doneifawaitrequest.is_disconnected():
service.remove_subscriber(sse_response)
sse_response=EventSourceResponse(remove_when_disconnected())
sevice.add_subscriber(sse_response)
returnsse_response
Thoughts: Is this a limitation of the python model, where using that reference in a different context/thread would be very dangerous? So we really do need to use thread safe things like queues.
Thanks
The text was updated successfully, but these errors were encountered:
I would like a reference to the SSE stream, so I can send messages to the user from anywhere else in my app. I would like to avoid using queues, polling or pubsub, as they seem a lot more complicated than storing a reference.
For example, when a user makes a request that returns an SSE stream, I would like to store that SSE stream somewhere (
service.add_subscriber(sse)
)This code won't work because
EventSourceResponse
doesn't have a function to publish to the client. There isstream_response
, but that seems to be about starting the stream, not just adding arbitrary messages.Thoughts: Is this a limitation of the python model, where using that reference in a different context/thread would be very dangerous? So we really do need to use thread safe things like queues.
Thanks
The text was updated successfully, but these errors were encountered: