grpc-web icon indicating copy to clipboard operation
grpc-web copied to clipboard

websocket transport could be killed while javascript is asleep.

Open vyredo opened this issue 2 years ago • 3 comments

Hi thanks for the great work. I encountered an issue with grpc-web when we used websocket as the transport layer. The problem is if user is on mobile device and put the app to background for 10 minutes more or so. When user active the tab again, the websocket already dead and since it's killed while Javascript is asleep. I believe the 'onTransportEnd' method don't run. I have a callback to reconnect websocket in when end is triggered. in this case the callback didn't run.

Is there anyway to get the status of websocket from the current implementation of grpc-web? The problem is quite common and easy to solve by exposing websockets, or just a method to get the status of websockets. For example:

  document.addEventListener("visibilitychange", () => {
         if(document.visibility === 'visible') {
               // get the status of each websocket connection and reconnect if it dies
         }
  })

vyredo avatar Jan 08 '23 13:01 vyredo

I faced the same problem. In addition to the end event, you can subscribe to the status event:

stream.on('status', function(status) {
  console.log(status.code);
  console.log(status.details);
  console.log(status.metadata);
});

In my case, when PC enters sleep mode, a status change is triggered with { code: 2, details: 'Http response at 400 or 500 level', ... }

I found the status table here.

Hope this helps you :)

ban4e avatar Jan 09 '23 12:01 ban4e

I think it's different . when PC enter a sleep mode, the websocket is killed before javascript is asleep.
For mobile device when user put app to background, the javascript is a sleep but websocket is still connected for some time and then it will be killed after few minutes while javascript is asleep.

vyredo avatar Jan 09 '23 22:01 vyredo

Maybe this also relates to #1363, where no end-event is sent if the network changes. Though I'm not sure, as it doesn't use websockets.

maja42 avatar Sep 19 '23 19:09 maja42