Why don't presence members leave as soon as I close a tab?

If you're using the javascript client, ably-js, you may have noticed that if you close a tab (or refresh the page etc.), it takes 15 seconds for any members which were present on a connection on that tab to leave the presence set.

 

By default the client library closes the connection when the page is closed or refreshed, which will cause the presence members to leave immediately. However, with some combinations of client options, this will not occur.

 

This can happen if:

  • You have explicitly set the closeOnUnload client option to false (for example, because you are using the connection state recovery feature). The option defaults to true.
  • You are using a version of ably-js old enough that closeOnUnload defaulted to false (in which case you should upgrade to the latest version)
  • You are using the `recover` client option, and have closed the tab. This option means that the same connection will be resumed after a page refresh (it stores the recovery key in the browser's localStorage), so the same presence members stay the whole time. But that does mean that if you close the tab rather than refreshing, the presence members will stick around in the presence set for 15 seconds (as the client library can not know if the beforeunload event is due to a refresh or a navigation / tab close).
  • You are using a browser that does not allow the beforeunload handler enough time to notify the server that the connection is closing when the tab is closed or refreshed. This is the case with some versions of Safari, Internet Explorer, and some other browsers
  • Chrome Memory saver has discarded the tab which doesn't fire a beforeunload event as described on ably-js unload page behaviour