fanout / django-eventstream

Server-Sent Events for Django
MIT License
664 stars 88 forks source link

Do I need close channels when all data are sent? #121

Closed scse-l closed 1 year ago

scse-l commented 1 year ago

I use format-channels for requests. If all data are sent, do I need close the channel? And how?

jkarneges commented 1 year ago

Typically, the server shouldn't need to care about the presence of connections. It simply generates events and if anyone is listening then they get the events. It is the client that decides whether it cares about listening. The most straightforward way to close an unneeded channel is from the client side.

That said, it is also possible to close connections from the server side, by using channel permissions. You could create a ChannelManager with a can_read_channel method that returns False for channels that will never have any more events. Then you could use channel_permission_changed to close connections.

scse-l commented 1 year ago

Typically, the server shouldn't need to care about the presence of connections. It simply generates events and if anyone is listening then they get the events. It is the client that decides whether it cares about listening. The most straightforward way to close an unneeded channel is from the client side.

That said, it is also possible to close connections from the server side, by using channel permissions. You could create a ChannelManager with a can_read_channel method that returns False for channels that will never have any more events. Then you could use channel_permission_changed to close connections.

Thanks for the reply! It's really helpful!