How to debug WebSocket / Server Sent Events reconnections on desktop?

Viewed 371

I am developing an app that uses Server Sent Events (SSE - basically one-way websockets). If the user uses the site on their mobile chrome browser and then tabs out for about a minute, the SSE connection breaks. If the user then tabs back into the site, it doesn't refresh the page (this is good). But I still need to re-establish the SSE connection so that the server can resume sending messages to them without a refresh.

I'm trying to debug my implementation but having to do it on mobile is very tedious. I have to grab my phone, refresh the page, then tab out, wait 1 minute (so the connection can break), and then tab back in to determine if my code for re-establishing the connection worked properly.

I would much rather be able to debug this on desktop, but I haven't found a way. I have tried the following:

  1. Tried 5 different Chrome extensions for sleeping a tab. Unfortunately, when you navigate back into the tab it just refreshes the page rather than resumes it.
  2. Tried using USB Debugging, but the desktop keeps the tab open at all times even if you tab out on mobile, so the tab never sleeps.
  3. Tried running debugger; in the Chrome Developer Tools console, but even if I let the Javascript sit with its execution frozen for 5+ minutes, it never breaks the SSE connection, so I can't test if reconnecting works.
  4. Tried using an extension to kill the internet for Chrome, but miraculously, this still maintains the active websocket/SSE connections. Apparently Google engineers have deprioritized this effort?

Basically, I am looking to simulate the behavior that happens on mobile when you tab out of your web browser, wait a minute, and then tab back in (the Javascript execution is frozen, the SSE connection is broken after a minute or so, and then tabbing back in resumes Javascript and attempts to reconnect the SSE connection).

Is this possible?

4 Answers

The most obvious solution to me would be to attach a listener that tracks the events related to tab activation and build some custom solution upon that.

Simply:

window.onfocus = () => { 
  // Restore SSE session 
}; 

window.onblur = function () { 
  // Inactive 
}; 

Just for info, SSE is a different protocol from websockets.
In case you are interested in using websockets implementation, I suggest the following library which works very well in my experience: https://www.npmjs.com/package/@stomp/stompjs

Stomp is an abstraction over the websocket protocol, but it also means you would need to implement this on the server side as well.

You can use the chrome://discards to freeze your tab.

You should close and restore the connection based on the Page LifeCycle events.

You Can Try to use pause execution

goto:

  • Chrome javascript console (Ctrl+Shift+J) > sources > pause script (press f8)

This simulates like a mobile tab switching so you can debug easily

I think this is the simple and easy solution. thanks

Related