sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
505 stars 36 forks source link

Explicit handling of refresh/tab-close in the examples #11

Closed alairock closed 3 years ago

alairock commented 3 years ago

Hopefully this will help clarity for future users of sse-starlette. This is an update to the documentation to show handling of connection closes due to refresh/tab-closing. Starlette doesn't disconnect the request, it cancels the future/task. Therefore checking for req.is_disconnected() won't ever get called and it will just die. Wrapping it all in a try/except CancelledError will keep the loop from dying (giving you time to cleanup, log, or anything else)

sysid commented 3 years ago

Thanks alairock for pointing this out. Just wondering, whether the req.is_disconnected() then actually serves any purpose at all. What do you think?

alairock commented 3 years ago

Just wondering, whether the req.is_disconnected() then actually serves any purpose at all. What do you think?

Sorry for the delay in response. I think it does still serve a purpose. If the browser refreshes or closed then starlette will kill the asyncio task the SSE response is sunning on.

But the connection managed on the client could still send a close() on their end (for whatever reason) and that will not cause starlette to kill the asyncio task, and I do believe is caught by req.is_disconnected().