Make socket auto disconnect on page hide optional#6198
Make socket auto disconnect on page hide optional#6198majeed-platogo wants to merge 1 commit intophoenixframework:mainfrom
Conversation
f0d39f0 to
82e69d7
Compare
assets/js/phoenix/socket.js
Outdated
| * setItem(keyName, keyValue) { this.storage[keyName] = keyValue } | ||
| * } | ||
| * | ||
| * @param {boolean} [opts.disableWindowEventListeners] - Boolean that determines if window event listeners should be active |
There was a problem hiding this comment.
If we add this, I'd rather call it something like disableAutoDisconnectOnPageHide to be explicit. LiveView passes unknown options to Phoenix and uses a lot of window event listeners, so this might be confusing.
cc @chrismccord
There was a problem hiding this comment.
Thank you for the feedback @SteffenDE. I've renamed the boolean
There was a problem hiding this comment.
Nice! One question: since this is the first time someone needs to adjust this, did you try just registering your pagehide / pageshow listener before you create your Socket and call stopImmediatePropagation in there?
window.addEventListener("pagehide", (e) => {
// prevent Phoenix from handling pagehide
e.stopImmediatePropagation();
// Custom code
...
});
const socket = new Socket(...);As that might be enough already?
I'd also be interested in what kind of stuff you do in pagehide, probably push some extra events?
There was a problem hiding this comment.
Hello @SteffenDE , No, I didn't do this.
This looks like a really good idea but I'm concerned that if there's some other package somewhere also listening for the pagehide event then this might cause some unexpected behaviour, right?
We do a bunch of tracking and also do some computations to determine the UI state when the user returns
82e69d7 to
53e516c
Compare
Co-authored-by: Tomislav Markanovic <tomislav@platogo.com>
53e516c to
25c8eb2
Compare
The block below is useful for handling autoconnection/disconnection when users leave or enter the tab
But in our case, It clashes with our own listener that does this among other things. That's why we're introducing
disableWindowEventListenersto make this optional