Recently, I was pulling out my hair facing a dilemma wherein the WebSocket connection was lost on an inactive Safari tab while using an iPad. First impression was a bug caused by Socket.IO. Upon investigation, I found that this was not the case, as every web socket connection was crashing and there was no way to detect this.
Upon deeper analysis, I gathered the following information:
The WebSocket Connection Loss issue is 100% reproducible and affects the Safari browser irrespective of the application environment.
Following is the simple description along with the steps to reproduce:
- Visit the page which connects to a WebSocket server.
- Switch away from the current tab/page.
- The WebSocket connection will stay open for some specific time. After 90 seconds (in my case) the connection will be closed by iOS automatically.
- Switch back to the previously visited tab/page and the Connection will be lost OR Safari will crash.
Further research revealed that this is a known issue as many developers have complained about this problem. As far as I understand, Apple does not believe the problem exists and hence they haven’t made any changes to fix this issue that I am aware of. My best guess is, Safari is dropping the socket connection for device efficiency and resource optimization.
- One of the possible ways to avoid this scenario is to detect iOS Safari browser and prevent WebSockets on them by rather using other transport methods. That may not be viable though.
- Another solution may be to apply a WebSocket timer while trying to reconnect within the specific time interval. Not a good solution either.
I would love to hear any fixes being rolled out in future for this one. Opinions on other effective workarounds you’ve used are welcome.
Browsers & Environments:
The issue was consistently reproducible on Safari browser run on iOS 5, 6 and 7 on iPads.
Interestingly, NO such issues were recorded on:
- iOS running Chrome
- Mac using Safari
- Windows tablet running IE
- Windows tablet using Chrome
- Windows laptop using Chrome, IE, Mozilla