Handling Network Change and connection events with useSubscription, Apollo Provider

I’m using an ApolloProvider that I pass in a client that I set up using:

 const httpLink = new HttpLink({ uri: url });
        const link = ApolloLink.from([
          createAuthLink({ url, region, auth }),
          createSubscriptionHandshakeLink({ url, region, auth }, httpLink),
        ]);
        const newClient = new ApolloClient({
          link: link,
          cache: new InMemoryCache(),
          connectToDevTools: true,
        });

The goal of using Apollo for me is to interact with AppSync and create a functional websocket that gets data on a certain mutation. Everything is working great thus far, but I was curious on two events:

  • Is there a way to set up a listener that lets the client know when the websocket itself is connected in using useSubscription?
  • Is there a way the client can know when the network connection is dropped, thus killing the websocket connection? (I’m seeing it resubscribe when the connection is restored, but I’d like to add an error bar letting users know when the network is out)

Thanks in advance for the help! I’m using useSubscription as follows:

  const { data: subscriptionData, error: subscriptionError } = useSubscription(
    my_subscription,
    {
      variables: { event: event },
    },
  );

I do see network status change events and documentation for queries, but not sure if this can be used for subscriptions or if there is something equivalent

Hi @skeletoncrew7 :wave: welcome to the forum! I think this is a question for the AWS AppSync folks - createSubscriptionHandshakeLink isn’t maintained by Apollo. Looking at their source code, though, this method stood out:

I think thereabouts is where you’d be able to listen to websocket events. Maybe from there throwing an ApolloError would do the trick? Please let me know if that helps unblock things :pray: