grpc / grpc-web

gRPC for Web Clients
https://grpc.io
Apache License 2.0
8.6k stars 765 forks source link

websocket transport could be killed while javascript is asleep. #1312

Open vyredo opened 1 year ago

vyredo commented 1 year ago

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
            }
     })
ban4e commented 1 year ago

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

vyredo commented 1 year ago

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.

maja42 commented 1 year ago

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.