Skip to content

Conversation

@majeed-platogo
Copy link

The block below is useful for handling autoconnection/disconnection when users leave or enter the tab

if (phxWindow && phxWindow.addEventListener) {
      phxWindow.addEventListener("pagehide", (_e) => {
        if (this.conn) {
          this.disconnect();
          awaitingConnectionOnPageShow = this.connectClock;
        }
      });
      phxWindow.addEventListener("pageshow", (_e) => {
        if (awaitingConnectionOnPageShow === this.connectClock) {
          awaitingConnectionOnPageShow = null;
          this.connect();
        }
      });
    }

But in our case, It clashes with our own listener that does this among other things. That's why we're introducing disableWindowEventListeners to make this optional

@majeed-platogo majeed-platogo force-pushed the change/optional-window-event-listeners branch from f0d39f0 to 82e69d7 Compare April 16, 2025 11:43
* setItem(keyName, keyValue) { this.storage[keyName] = keyValue }
* }
*
* @param {boolean} [opts.disableWindowEventListeners] - Boolean that determines if window event listeners should be active
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback @SteffenDE. I've renamed the boolean

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@majeed-platogo majeed-platogo force-pushed the change/optional-window-event-listeners branch from 82e69d7 to 53e516c Compare December 12, 2025 08:58
Co-authored-by: Tomislav Markanovic <tomislav@platogo.com>
@majeed-platogo majeed-platogo force-pushed the change/optional-window-event-listeners branch from 53e516c to 25c8eb2 Compare December 12, 2025 09:02
@majeed-platogo majeed-platogo changed the title Make phxWindow.addEventListener optional Make socket auto disconnect on page hide optional Dec 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants