Reconnect websocket on window focus as a solution to disconnects on mobile devices#366
Merged
picklelo merged 8 commits intoreflex-dev:mainfrom Jan 28, 2023
Merged
Reconnect websocket on window focus as a solution to disconnects on mobile devices#366picklelo merged 8 commits intoreflex-dev:mainfrom
picklelo merged 8 commits intoreflex-dev:mainfrom
Conversation
picklelo
approved these changes
Jan 28, 2023
| " }}", | ||
| f" if (typeof {SOCKET}.current !== 'undefined') {{{{", | ||
| f" if (!{SOCKET}.current) {{{{", | ||
| f" window.addEventListener('focus', reconnectSocket)", |
ACucos1
pushed a commit
to ACucos1/rd-pynecone
that referenced
this pull request
Feb 28, 2023
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
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.
There appears to be some issue with ReconnectingWebSocket (and likely the base websocket) that causes onclose onerror events not to tigger when the device hibernates the window, especially on iOS Safari, preventing syncing of the front-end unless the page is refreshed.
Instead of refreshing the page the best and simplest solution I've found is to simply reconnect the current websocket on window focus. This happens quickly and is seamless and ensures there's always a live websocket available.
It isn't the perfect approach but it solves the problem until perhaps a different websocket library (such as Websocket.IO) is used.
You may be wondering what
{SOCKET}.current !== 'undefined'is doing.Diagnosing the problem and trying to reproduce it is sometimes hard because there's different ways in which for e.g. sleeps tabs/pages. It might be voodoo, but I've found performing this check seems to make the fix work more consistently. I have no idea why that would be the case so it might be unnecessary.