enisdenjo / graphql-ws

Coherent, zero-dependency, lazy, simple, GraphQL over WebSocket Protocol compliant server and client.
https://the-guild.dev/graphql/ws
MIT License
1.75k stars 162 forks source link

Socket could not reconnect after IOS app comeback from background #487

Closed sonnd08 closed 1 year ago

sonnd08 commented 1 year ago

Hi, we are using graphql-ws in an app using react-native-webview. It actually work quite well untill we switch to another app or put the app in background mode.

Expected Behaviour

  1. The socket should keep reconnect or at least try to re-connect/re-init if needed.
  2. In case re-connect/re-init, we would like to run a specific function to resync data after connect successfully.

Actual Behaviour All subscription stop working and could not work even when we tried to resubscibe. We got this after reopen the app.

WebSocket connection to 'wss://my-domain.com/subscription' failed: The operation couldn’t be completed. (kNWErrorDomainPOSIX error 53 - Software caused connection abort)

TCL: -> Socket closed -> code: – 1006

Further Information

this.client = createClientWithOnReconnected({
      ...options,
      keepAlive: 5000, // send ping message every 5s
      retryWait: () => 2000,
      retryAttempts: 30,
      on: {
        connecting: () => console.log("Connecting to WebSocket"),
        connected: (socket) => {
          console.log("Connected to WebSocket server", socket);
        },
        error: (code, reason) => {
          console.log(`Socket error:`, { code, reason });
        },
        closed: ({ code, reason }) => {
          console.log(`Connection closed:`, { code, reason });
          if (code === 1006) {
            // this unsubscribe all existing subscription and resubscribe them
            // when asking new subscription and no subscriptions existed, it will try to create new socket connection
            reRegisterAllSubsciption()
          }
        },
      },
    });

    this.client.onReconnected(() => {
      console.log("Socket: onReconnected");
      refreshData()
    });