Reconnect doesn't work after Safari drops WebSocket connection when the user locks the screen on a mobile device. #290
-
Repro steps
The issue might be reproduced on iPad/iPhone running Safari browser. It seems Safari freeze the app memory and socket itself.. https://stackoverflow.com/questions/24796103/keep-websocket-alive-in-mobile-safari Expected Behaviour Actual Behaviour Further Information
Before Moreover I found workaround like this. I'm not sure it's the best one. I would prefer
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 14 replies
-
This is certainly an issue with Safari. If the OS/browser does not communicate a close event, there's absolutely no way Furthermore, the
The way you handle it is the way I'd recommend handling it. Please keep in mind that
Can you elaborate a bit more on how does |
Beta Was this translation helpful? Give feedback.
-
This problem is specific to Safari and seems like NoSleep.js does the trick of keeping the socket alive, but it comes with a cost as described in #290 (reply in thread).
|
Beta Was this translation helpful? Give feedback.
-
Thanks for this folks, can you please edit: nvm, figured it out: let timedOut: any;
const wsLink = new GraphQLWsLink(
createClient({
url: "wss://xxxxx",
lazy: true,
on: {
ping: (received) => {
if (!received /* sent */) {
timedOut = setTimeout(() => {
// a close event `4499: Terminated` is issued to the current WebSocket and an
// artificial `{ code: 4499, reason: 'Terminated', wasClean: false }` close-event-like
// object is immediately emitted without waiting for the one coming from `WebSocket.onclose`
//
// calling terminate is not considered fatal and a connection retry will occur as expected
//
// see: https://github.com/enisdenjo/graphql-ws/discussions/290
wsLink.client.terminate();
}, 5_000);
}
},
pong: (received) => {
if (received) {
clearTimeout(timedOut);
}
}, |
Beta Was this translation helpful? Give feedback.
This problem is specific to Safari and seems like NoSleep.js does the trick of keeping the socket alive, but it comes with a cost as described in #290 (reply in thread).
I will be looking into integrating a solution intoPlease consult the "Client usage with abrupt termination on pong timeout" recipe.graphql-ws
; however, this is not that straight forward as mentioned briefly in #290 (comment).