Reconnect websocket on window focus as a solution to disconnects on mobile devices #366
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.