Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens with WebSocket connections when a phone's screen locks?

When a phone browser has an open connection, and the user locks the screen, then at a certain point they will no longer have a WebSocket connection.

What events are fired when this happens? Is the WebSocket.onerror or WebSocket.onclose handler called, and if so, does this happen when the screen locks/the app is suspended, or when the app comes back up again?

(And bonus question: is this standardised, or do browsers behave differently, and if so, how?)

like image 699
Vincent Avatar asked Oct 02 '18 12:10

Vincent


People also ask

What can block WebSocket connection?

A blocked connection can be caused by: AdBlocker / Cookie blocker browser extensions. Antivirus and Firewall software. Proxy and VPN connections.

Does WebSocket keep connection open?

WebSocket is a bidirectional communication protocol that can send the data from the client to the server or from the server to the client by reusing the established connection channel. The connection is kept alive until terminated by either the client or the server.

What causes WebSockets to close?

This is likely due to client security rules in the WebSocket spec to prevent abusing WebSocket. (such as using it to scan for open ports on a destination server, or for generating lots of connections for a denial-of-service attack).

Does WebSockets work on mobile?

You can decide to use any WebSocket-based protocol that supports Android. Regardless of your choice, though, you need to consider the entire spectrum of challenges you'll face when it comes to WebSockets.


1 Answers

I've done some testing myself, and the answer seems to be: no events are fired. Although the connection does drop, no error or close events are fired, not even when the browser comes back up. Therefore, the main way to deal with this appears to be to periodically check the connection status, and reconnect if need be - with exponential back-off in case the connection drops server-side. (Or to have a library do this for you, though I haven't found a properly maintained client-side browser-based WebSocket library that does this yet.)

This seems corroborated by the author of this article:

Mobile devices introduce a new category of connection issues; if 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.

like image 74
Vincent Avatar answered Sep 30 '22 15:09

Vincent