1

Anybody is aware about a method to automatically unsubscribe/remove a user from a Twilio Programable Chat channel, after some time of inactivity ?

The most simple scenario that comes to my mind is the one where a user closes the browser tab without leaving the chat channel (so channel.leave() is never called)... and as a consequence stays as a member of the channel forever.

Another scenario would be when network goes down.

colxi
  • 7,640
  • 2
  • 45
  • 43
  • There's nothing built into the Twilio SDK to handle this, so you'd need to build this yourself. You can try to perform `channel.leave()` when the [user closes the browser tab](https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing). That doesn't solve the lack of network though. You could built a background job that periodically runs through your channels and removes users that haven't been active. What have you tried for this so far? – philnash Oct 08 '19 at 00:09
  • I'm currently using `WindowEventHandlers.onbeforeunload` to handle the tab/browser close event and trigger a `channel.leave()` . For the unhandled scenarios (network/power down) i have only dirty workarounds in the client side. – colxi Oct 08 '19 at 13:29
  • The issue here is that chat is built to expect users to stay part of the channel, even if they go offline. I would probably go for the background job to clear things up after a set time, rather than client side workarounds, as they can't be guaranteed to run. – philnash Oct 08 '19 at 21:45
  • Server side activity monitoring tasks is exactly what i was trying to avoid. But it looks like is the only possible approach, considering that Twilio does not provide a mechanism to automate this. Thanks for your answers – colxi Oct 09 '19 at 13:00

1 Answers1

0

By using beforeunload event, you can do something like this: (I'm using react here)

// Things to do before unloading/closing the tab
doSomethingBeforeUnload = () => {
    if (this.room) {
        this.room.disconnect()
        // Detach other things such as participant video, if you need
    }
}

// Setup the `beforeunload` event listener
setupBeforeUnloadListener = () => {
    window.addEventListener("beforeunload", (ev) => {
        ev.preventDefault();
        return this.doSomethingBeforeUnload();
    });
};

componentDidMount() {
    // Activate the event listener
    this.setupBeforeUnloadListener();
}
Jay Patel
  • 2,341
  • 2
  • 22
  • 43
  • `beforeunload` is not reliable for network requests, most of them will fail due the tab process being terminated before the request is completed – colxi Aug 08 '20 at 16:32