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:
- is that is does not catch when the user disconnects from their internet (only from when the server restarts for whatever reason)
- that the reconnect is triggered outside of my React apps ponents.
What I would like to be able to do is to is reload my "chat" ponent 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 ponent to pletely reload which i'm not sure would be possible from outside my ponent tree.
Is there a method in the Query
or Subscription
Apollo ponents to be able to capture this event and handle it accordingly from the ponent?
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:
- is that is does not catch when the user disconnects from their internet (only from when the server restarts for whatever reason)
- that the reconnect is triggered outside of my React apps ponents.
What I would like to be able to do is to is reload my "chat" ponent 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 ponent to pletely reload which i'm not sure would be possible from outside my ponent tree.
Is there a method in the Query
or Subscription
Apollo ponents to be able to capture this event and handle it accordingly from the ponent?
2 Answers
Reset to default 1There 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.
- Setup a
online/offline
listener (ref) - Setup an
Apollo middleware
to handle network errors from your server (ref) - 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 ofisOnline
- Finally, to bundle all of it together. Create a react HOC which uses
isOnline
to handle the network state for each ponent. This can be used to handle network error messages, refresh ponents once network is restored.
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.