Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: can I store a reference to an SSE connection? #92

Closed
ben-xD opened this issue Mar 14, 2024 · 1 comment
Closed

Question: can I store a reference to an SSE connection? #92

ben-xD opened this issue Mar 14, 2024 · 1 comment

Comments

@ben-xD
Copy link

ben-xD commented Mar 14, 2024

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")
async def get_sensor_data_sse(request: Request, service: SensorDataService = Depends(SensorDataProvider.get)) -> EventSourceResponse:
    async def remove_when_disconnected():
        # remove when its done
        if await request.is_disconnected():
            service.remove_subscriber(sse_response)
    
    sse_response = EventSourceResponse(remove_when_disconnected())
    sevice.add_subscriber(sse_response)
    return sse_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

@sysid
Copy link
Owner

sysid commented Jun 5, 2024

No community response, closed due to inactivity.

@sysid sysid closed this as completed Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants