socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Event triggered on Connection State Recovery Expiration

Open bryghtlabs-richard opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe.

Per-connection, we're keeping a large object in memory, which we would prefer to use across connections that are recovered through connection-state-recovery. Connection state recovery solves one problem for us, in that we can reconnect a client to its object, but this makes it harder to know when to delete the object.

Describe the solution you'd like

When a connection exceeds the connection state recovery maxDisconnectionDuration, I'd like an event trigger with the SocketID as an argument.

Describe alternatives you've considered

We've considered setting an additional timeout, but this won't ever be as accurate - we'd have to make this timeout longer than maxDisconnectionDuration, and deal with our large staying around longer than needed.

bryghtlabs-richard avatar Dec 04 '24 16:12 bryghtlabs-richard

Hi! That's an interesting idea :+1:

I think implementing this for the in-memory adapter would be quite straightforward.

For the adapter based on Redis streams, we would need to listen to the "expired" events (ref), though it would not be reliable if the Redis client disconnects.

Not sure how we could implement it for the adapter based on MongoDB though...

Regarding the API, would something like this suit your needs?

io.of("/").adapter.on("sessionExpiry", (sid) => {
  // ...
});

darrachequesne avatar Dec 06 '24 08:12 darrachequesne

Regarding the API, I must confess I'm quite new to JS, but it looks good to me. We'd use that handler to destroy the corresponding large object paired to sid.

One question - with the in-memory adapter, without connection-state-recovery, would this event still be received?

bryghtlabs-richard avatar Dec 06 '24 15:12 bryghtlabs-richard