SocketCluster / socketcluster

Highly scalable realtime pub/sub and RPC framework
https://socketcluster.io
MIT License
6.15k stars 314 forks source link

Can I listen any event just before any channel is garbage collected i.e. When we have Zero subscribers to a channel? #551

Closed Shankar-khati closed 2 years ago

Shankar-khati commented 3 years ago

My use case is simple when the conversation on an SC cluster gets completed I need to dump all data from the cache to persistent. But for that, I'm looking for an event or callback which could be executed just before the channel is garbage collected.

Shankar-khati commented 3 years ago

@jondubois, @MegaGM, @sacOO7 can anyone give me any suggestion for the above statement?

jondubois commented 3 years ago

@Shankar-khati Sorry for the delay in answer. Maybe you figured it out already. You can use the 'unsubscribe' event on the server socket (on a per-socket basis); it triggers when either:

See https://socketcluster.io/docs/api-ag-server-socket/

Alternatively, you can listen to the 'unsubscription' event on the server (it aggregates all the 'unsubscribe' events from all sockets which are attached to that server/process) - It allows you to perform the unsubscription handling for all sockets and channels from a central place (instead of having to register the handler on a per-socket basis). See https://socketcluster.io/docs/api-ag-server/

Shankar-khati commented 3 years ago

Thanks @jondubois for reply. I followed the same but I was looking for some hook or event which we can listen. :)

jondubois commented 3 years ago

@Shankar-khati I don't understand. You can listen to that event using a for-await-of loop.

Shankar-khati commented 2 years ago

@jondubois currently I'm listen on subscribe and unsubscribe event on server and maintaining a counter for each channel at my end, just wanted to know if you guys have any event from scc-broker-client or scc-state where I can expect an event when a Channel submitted to garbage collector or when the first subscription happen to the channel.