Closed firatkucuk closed 3 years ago
I have the same question.
I already am checking request.is_disconnected()
but notice when client connection is "lost", it still sends PING events (see backend_1 | TRACE: 172.24.0.3:39602 - Connection lost
that happens just before the last 2 pings:
I wonder if these issues are at all relevant:
https://github.com/encode/uvicorn/issues/388 Discussion starting in this comment
The more I read this discussion and comparing it to the current code --- the more I think this is a bug?
What do you think @sysid ?
Thanks @paxcodes for your analysis, much appreciated. I tend to agree but I currently I cannot dig deeper into it. If you have an idea for a quick fix, please let me know.
I will try to find time asap, though.
PS: Do you think this might be a way to follow? https://github.com/encode/starlette/blob/b3271a53b0b5c98ef3117dc0b82bd7f700fb44a8/starlette/responses.py#L216
Exactly. I was looking at that.
Is there a reason why we are not simply inheriting from StreamingResponse
similar to what this person did? IIUC, if we inherit StreamingResponse, this scenario will be taken care of.
Done. Pretty much followed your proposition. Thanks again! #10
Thanks so much, @sysid for responding so quickly. It does fix my use case. Any chance you'll publish a release soon?
By the way, I realized yesterday that the /endless
endpoint works perfectly even before this PR was merged. That is, the server stops streaming once the client disconnects.
I was simply not using request.is_disconnected() properly:
async streamMessages():
while True:
disconnected = await req.is_disconnected()
if disconnected:
break
async with broadcast.subscribe(channel=room.code) as subscriber:
async for event in subscriber:
msg = event.message
yield msg
return EventSourceResponse(streamMessages())
But the merge was still beneficial since I can now do this without having to worry about client disconnection (since this package takes care of it already):
async streamMessages():
async with broadcast.subscribe(channel=room.code) as subscriber:
async for event in subscriber:
msg = event.message
yield msg
return EventSourceResponse(streamMessages())
@paxcodes if you don't mind, can you share code broadcast
, I am trying to send a notification when user creates or updates comments. the code you mentioned above looks good.
@paxcodes if you don't mind, can you share code
broadcast
, I am trying to send a notification when user creates or updates comments. the code you mentioned above looks good.
I'm assuming @paxcodes is using redis or similar package
https://redislabs.com/ebook/part-2-core-concepts/chapter-3-commands-in-redis/3-6-publishsubscribe/
Thanks for chiming in @UrbanSwati -- I emailed vaibhavmule as I don't want to hi-jack this issue's thread.
The client can unexpectedly disconnect but I couldn't find an easy way of doing that.