sysid / sse-starlette

BSD 3-Clause "New" or "Revised" License
544 stars 37 forks source link

Cleanup on client disconnect #97

Closed sergpsu closed 2 months ago

sergpsu commented 4 months ago

Hi guys. Need a proper way to make some cleanups after client disconnects. What I've implemented looks like this:

class MyEventSourceResponse(EventSourceResponse):
    user_id: uuid.UUID

    def __init__(self, user_id: uuid.UUID, *args, **kwargs):
        self.user_id = user_id
        super().__init__(*args, **kwargs)

    # @staticmethod
    async def listen_for_disconnect(self, receive: Receive) -> None:
        while True:
            message = await receive()
            if message["type"] == "http.disconnect":
                #------------------------ DOING CLEANUP BASED ON self.user_id -------------------------
                break

So listen_for_disconnect cannot be staticmethod and this is not a clean way to subclass. Is there a better way?

sysid commented 2 months ago

Closed due to inactivity.