How to recover from a WebSocket client computer to sleep or to the background app (Safari on iPad)

I have a Javascript browser that opens a WebSocket (using socket.io) to request a long run of the process and then receives a callback when the process is running. When I receive a callback, I refresh the webpage so that the user knows that the process is complete.

This works fine, with the exception of my iPad, when I switch to another application and then return (it never gets a callback because I assume the application is not working at that time). I guess the same thing will happen on a laptop or another computer that is sleeping, waiting for a callback.

Is there a standard way (or in any way) to solve this scenario? Thanks.

For reference, if you want to see the problem page, it is at http://amigen.perfectapi.com/

+6
source share
1 answer

In this case, there are a few things to consider:

Determine that the application is disabled / enabled

See: Online and offline events .

When your application detects an online event after waking up the computer, you can get any information that you missed.

For older web browsers, you need to do this in a smarter way. At Pusher, we added ping-pong checks between the client and server. If the client does not receive ping for a certain period of time, he knows that there is a connection problem. If the server sends ping and does not receive pong back with a certain time, it knows what the problem is.

The ping-pong mechanism is defined in the specification , but the method of sending a ping or pong has not been defined in the WebSocket API as yet.

Getting Lost Information

Most real-time servers only deliver messages for connecting to clients. If the client is not connected, perhaps due to a temporary network outage or their computer is sleeping for a while, then these clients will skip the message.

Some structures provide access to messages through history / cache. For those who do not need this, you will need to detect the problem (as indicated above) and then receive any missed messages. A good way to do this is to provide a timestamp or sequence identifier with each message so that you can call your web server to say “give me all messages with X”.

+7
source

Source: https://habr.com/ru/post/905922/


All Articles