How to check/detect websocket connection after long inactivity or reopening the app from background?

I rely on LiveQuery for many projects: it’s the strongest Parse feature.

I use LiveQuery client’s and subscriptions’ on open, close, and error events to detect WebSocket connection changes. Unfortunately, if the client (or the browser) is in background, I’m not receiving any close or error event.

For example on a web app, if you close your laptop and reopen it after a few hours, close/error event. is not fired and the client doesn’t immediately know that the connection is closed. Same issue on mobile.

What is the best way to force check the WebSocket status or renew the subscription?

Thanks

1 Like

You should receive another open event (JavaScript Developers Guide | Parse). Isn’t that happening?

It’s happening but just after several seconds which is not a reasonable time for the kind of app that I’m building. Also, I’m not sure it’s happening 100% of the times.

I’ve made some research and found that it’s impossible to promptly detect Websocket loss. Websocket is relying on signals, and if the connection is interrupted, the signal is not sent/received.

The only option seems to be a ping-pong or a keepalive.

I’m trying to understand if there is an easy way to implement a ping-pong. I tried to send a random message to the LiveQuery server but it doesn’t respond to unrecognized messages.

In order to implement ping:

  1. the server should handle “ping” messages and send a reply
  2. more in general, it would be great to support hooks to WS messages in order to implement custom responses to custom messages
  3. the clients should have a ping feature, or the flexibility to send custom messages and subscribe to custom responses

Is there a better option?

Meanwhile, i’ll just create a class, make a query, subscribe to it, modify an object every second (with object.save()), and montor the LiveQuery response.

That’s what I believe it is happening in your case: the app is going to background and therefore loosing the ws connection. When the is becoming active again, the ws is reconnected and open event is fired. I don’t see how the ping pong would help in this scenario.

The issue is this: When the app is becoming active again, it takes up to two minutes to have the connection working again automatically, and even worse: the user doesn’t know it because there is no way to get this info.

When the app goes back active I need to show immediate feedback and try to restore the connection asap without waiting for automatic recovery.

Thanks

This is the current implementation for js sdk to reconnect to web socket: Parse-SDK-JS/LiveQueryClient.js at alpha · parse-community/Parse-SDK-JS · GitHub

It will take up to 30s to try again depending on how many attempts were already tried. I believe that it will not keep trying in background (not tested that) and it should retry immediately after becoming active.

Anyways, any improvement should be done in this part of the sdk code.

I’ve found that websocket PING should implemente in Parse:
the option is called websocketTimeout in LiveQueryServerOptions

Apparently, this is not working on my side