red house 87
red house 87

Reputation: 2415

Apollo subscriptions - handling WS disconnects with subscribeToMore

I've been looking for a way to handle web socket disconnects in my React app with Apollo subscriptions and have not found a way to do so. The other examples I see in the apollo documentation show the below method for catching a reconnect:

  const wsClient = process.browser ? new SubscriptionClient(WSendpoint, {
    reconnect: true,
  }) : null;

  const wsLink = process.browser ? new WebSocketLink(wsClient) : null;

  if (process.browser) {
    wsLink.subscriptionClient.on(
      'reconnected',
      () => {
        console.log('reconnected')
      },
    )
  }

There are two issues with the above method:

  1. is that is does not catch when the user disconnects from their internet (only from when the server restarts for whatever reason)
  2. that the reconnect is triggered outside of my React apps components.

What I would like to be able to do is to is reload my "chat" component if the user either disconnects from their internet or if my express server goes down for any reason. For this to happen I would need my chat component to completely reload which i'm not sure would be possible from outside my component tree.

Is there a method in the Query or Subscription Apollo components to be able to capture this event and handle it accordingly from the component?

Upvotes: 14

Views: 4894

Answers (2)

lpereira
lpereira

Reputation: 11

You can use SubscriptionClient callbacks from subscriptions-transport-ws, like this:

const ws = require("ws");
const { SubscriptionClient } = require("subscriptions-transport-ws");
const { WebSocketLink } = require("apollo-link-ws");
const { ApolloClient } = require("apollo-client");
const { InMemoryCache } = require("apollo-cache-inmemory");

const subClient = new SubscriptionClient(
    'ws://localhost:4000/graphql',
    { reconnect: true },
    ws
);

subClient.onConnected(() => { console.log("onConnected") });
subClient.onReconnected(() => { console.log("onReconnected") });
subClient.onReconnecting(() => { console.log("onReconnecting") });
subClient.onDisconnected(() => { console.log("onDisconnected") });
subClient.onError(error => { console.log("onError", error.message) });

const wsLink = new WebSocketLink(subClient);

const client = new ApolloClient({
    link: wsLink,
    cache: new InMemoryCache()
});

I'm using this for Node.js, but it will probably work for React too.

Upvotes: 1

varun agarwal
varun agarwal

Reputation: 1509

There are a few ways I can think of to handle these cases but none of them are a one-shot solution, each case needs to be handled independently.

  1. Setup a online/offline listener (ref)
  2. Setup an Apollo middleware to handle network errors from your server (ref)
  3. Create a variable in your store, isOnline for example, which can hold a global reference of your app's state. Whenever the above two methods trigger, you could update the value of isOnline
  4. Finally, to bundle all of it together. Create a react HOC which uses isOnline to handle the network state for each component. This can be used to handle network error messages, refresh components once network is restored.

Upvotes: 1

Related Questions