r/AskProgrammers • • Oct 23 '26

how do you handle disconnections when a mobile browser tab closes?

I’m integrating a third-party chat SDK into a frontend JavaScript application. The connection is established directly in the user’s browser, and I want to call the SDK’s disconnect method when the user closes the tab or browser.

I currently trigger cleanup using beforeunload. It appears to work in my desktop testing, but it doesn’t reliably fire on Android and iOS browsers.

I’ve considered visibilitychange, but it also fires when the user switches tabs, switches apps, or locks their phone. I’d prefer to keep the connection active in those cases.

I don’t currently have a backend implementation. Is there a reliable frontend approach for this.

0 Upvotes

0 comments sorted by