-
Notifications
You must be signed in to change notification settings - Fork 3k
Stop reconnecting when page is hidden #6534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
+28
−6
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This tries to address the problem of seeing the "attempting to reconnect" flash lingering around for a very long time when resuming a LiveView tab on mobile. In my tests, this mostly happens when fallback to longpoll is disabled. In this case, the following happens: 1. User puts their phone to sleep. 2. After a couple of seconds, the browser kills the websocket connection and Phoenix registers this as an error. 3. Phoenix tries to establish a new WebSocket connection. This hangs. 4. User opens the page again. Instead of immediately establishing the connection, the WebSocket is stalled for a while. The exact amount seems a bit random, but I've seen 10+ seconds. During that time, the WebSocket in the devtools doesn't show any activity, but then suddenly the connection works again. If LongPoll is active, at point 3, Phoenix instead tries a LongPoll connection. In mobile Chrome, the HTTP request fails and Phoenix registers this as 500 and tries again, backing off. If the user resumes the page - depending on their luck - they might need to wait for the next backoff attempt to fire, then the connection is established quickly. This PR addresses this problem by checking the visibility state using the `visibilitychange` event, which is recommended for registering a tab going in the background (for example by a phone going to sleep). We set a flag that we're hidden and in the case that there's an error, we don't try to reconnect immediately (since we're hidden). Instead, we reconnect as soon as the visibilitychange fires again with the page not hidden any more. With this change, the connection is immediately established in both mobile Chrome (Android) and Mobile Safari (iOS), most of the time not seeing the "trying to reconnect" flash at all. Also, this PR addresses a problem where the `connectWithFallback` function would accumulate `onOpen` listeners, causing duplicate callbacks. Hopefully closes phoenixframework/phoenix_live_view#3896. Hopefully closes #6149.
josevalim
approved these changes
Nov 13, 2025
Member
josevalim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Beautiful!
filipecabaco
pushed a commit
to supabase/phoenix
that referenced
this pull request
Nov 26, 2025
* Stop reconnecting when page is hidden This tries to address the problem of seeing the "attempting to reconnect" flash lingering around for a very long time when resuming a LiveView tab on mobile. In my tests, this mostly happens when fallback to longpoll is disabled. In this case, the following happens: 1. User puts their phone to sleep. 2. After a couple of seconds, the browser kills the websocket connection and Phoenix registers this as an error. 3. Phoenix tries to establish a new WebSocket connection. This hangs. 4. User opens the page again. Instead of immediately establishing the connection, the WebSocket is stalled for a while. The exact amount seems a bit random, but I've seen 10+ seconds. During that time, the WebSocket in the devtools doesn't show any activity, but then suddenly the connection works again. If LongPoll is active, at point 3, Phoenix instead tries a LongPoll connection. In mobile Chrome, the HTTP request fails and Phoenix registers this as 500 and tries again, backing off. If the user resumes the page - depending on their luck - they might need to wait for the next backoff attempt to fire, then the connection is established quickly. This PR addresses this problem by checking the visibility state using the `visibilitychange` event, which is recommended for registering a tab going in the background (for example by a phone going to sleep). We set a flag that we're hidden and in the case that there's an error, we don't try to reconnect immediately (since we're hidden). Instead, we reconnect as soon as the visibilitychange fires again with the page not hidden any more. With this change, the connection is immediately established in both mobile Chrome (Android) and Mobile Safari (iOS), most of the time not seeing the "trying to reconnect" flash at all. Also, this PR addresses a problem where the `connectWithFallback` function would accumulate `onOpen` listeners, causing duplicate callbacks. Hopefully closes phoenixframework/phoenix_live_view#3896. Hopefully closes phoenixframework#6149. * make tests happy
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This tries to address the problem of seeing the "attempting to reconnect" flash lingering around for a very long time when resuming a LiveView tab on mobile. In my tests, this mostly happens when fallback to longpoll is disabled. In this case, the following happens:
If LongPoll is active, at point 3, Phoenix instead tries a LongPoll connection. In mobile Chrome, the HTTP request fails and Phoenix registers this as 500 and tries again, backing off. If the user resumes the page - depending on their luck - they might need to wait for the next backoff attempt to fire, then the connection is established quickly.
This PR addresses this problem by checking the visibility state using the
visibilitychangeevent, which is recommended for registering a tab going in the background (for example by a phone going to sleep). We set a flag that we're hidden and in the case that there's an error, we don't try to reconnect immediately (since we're hidden). Instead, we reconnect as soon as the visibilitychange fires again with the page not hidden any more.With this change, the connection is immediately established in both mobile Chrome (Android) and Mobile Safari (iOS), most of the time not seeing the "trying to reconnect" flash at all.
Also, this PR addresses a problem where the
connectWithFallbackfunction would accumulateonOpenlisteners, causing duplicate callbacks.Hopefully closes phoenixframework/phoenix_live_view#3896.
Hopefully closes #6149.
If someone wants to test this, you can use: