'websocket conecttion closed (on mobile browsers) when screen looks

I have a connection to WebSocket which sends me data every second, but when I lock the screen of my phone, after 5 min (approximately), the WebSocket connection gets closed with code 1006.

That only happens when locking the screen. If I open another tab, everything is okay.



Solution 1:[1]

It happens because a mobile device is locked, goes to sleep or the application is moved to the background, an active WebSocket connection may become unresponsive and not close itself properly.

There is no way you can prevent this behaviour w/o forcing your browser to keep working in the background.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1