Skip to content

Reconnect websocket on window focus as a solution to disconnects on mobile devices#366

Merged
picklelo merged 8 commits intoreflex-dev:mainfrom
thavocado:main
Jan 28, 2023
Merged

Reconnect websocket on window focus as a solution to disconnects on mobile devices#366
picklelo merged 8 commits intoreflex-dev:mainfrom
thavocado:main

Conversation

@thavocado
Copy link
Contributor

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.

Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

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

Sweet!

" }}",
f" if (typeof {SOCKET}.current !== 'undefined') {{{{",
f" if (!{SOCKET}.current) {{{{",
f" window.addEventListener('focus', reconnectSocket)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Good find

@picklelo picklelo merged commit 0056d94 into reflex-dev:main Jan 28, 2023
ACucos1 pushed a commit to ACucos1/rd-pynecone that referenced this pull request Feb 28, 2023
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.

3 participants