Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have browser client Javascript which opens a WebSocket (using socket.io) to request a long-running process start, and then gets a callback when the process is done. When I get the callback, I update the web page to let the user know the process has completed.

This works ok, except on my iPad when I switch to another app and then come back (it never gets the callback, because I guess the app is not online at the time). I'm assuming the same thing will happen on a laptop or other computer that sleeps while waiting for the callback.

Is there a standard way (or any way) to deal with this scenario? Thanks.

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

like image 384
Steve Campbell Avatar asked Jan 13 '12 15:01

Steve Campbell


People also ask

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).

How do I stop WebSocket client?

In python websockets, you can use "ws. keep_running = False" to stop the "forever running" websocket.

Can WebSockets be blocked?

These are the so-called Websocket connections (more information at the bottom of this article). A blocked connection can be caused by: AdBlocker / Cookie blocker browser extensions. Antivirus and Firewall software.


1 Answers

There are a couple of things to consider in this scenario:

Detect the app going off/on line

See: Online and offline events.

When your app detects the online event after the computer wakes up you can get any information that you've missed.

For older web browsers you'll need to do this in a cleverer way. At Pusher we've added a ping - pong check between the client and server. If the client doesn't receive a ping within a certain amount of time it knows there's a connection problem. If the server sends a ping and doesn't get a pong back with a certain time it knows there's a problem.

A ping pong mechanism is defined in the spec but a way of sending a ping or pong hasn't been defined on the WebSocket API as yet.

Fetching missed information

Most realtime servers only deliver messages to connected to clients. If a client isn't connected, maybe due to temporary network disturbance or their computer has been asleep for a while, then those clients will miss the message.

Some frameworks do provide access to messages through a history/cache. For those that don't you'll need to detect the problem (as above) and then fetch any missed messages. A good way to do this is by providing a timestamp or sequence ID with each messages so you can make a call to your web server to say "give me all messages since X".

like image 99
leggetter Avatar answered Oct 15 '22 01:10

leggetter