0

WS Connection opens and stays open for quite a long time - which is great. If you leave the page for a long time or the computer goes to sleep/hibernation, the WS connection closes and is never re-established.

The use-case of my app is that the users should hear a "ping" noise when a new notification comes. But as the web socket gets disconnected, this does not happen. Maybe the browser closes the connection in background.

As this problem seems to be a general one, how is this handled in industry-ready web-apps?

One follow up: In case of internet connection toggle, can websockets be retried in background if the user hasn't opened the app?

1 Answer 1

1

When setting up Web Sockets, depending on the tool used, you also have to check for a timer option.

Another possible solution is to keep the connection alive by doing a ping call periodically.

3
  • Do I need to use a service worker or something for this so that the connection stays active for pinging? I think (I may be wrong) the browser closes the connection because the tab is in the background. Commented Jul 2 at 16:11
  • 1
    Websockets are kept alive 180 seconds by default, and are closed is no calls are made through the channel. Why do you need to keep the connection alive? It is not bad UX to do or force a refresh. Commented Jul 2 at 17:30
  • 1
    Ohh I see. Makes sense - Will try that out. Thanks a lot! Commented Jul 2 at 18:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.