-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client.stop() should call close on websocket link #7257
Comments
I reread the code and nothing was added to call the |
While we're waiting for an official implementation (and for anyone who's looking at the issue apollographql/apollo-link#197 and still wondering how to refresh import * as React from "react";
import { ApolloClient, HttpLink, InMemoryCache, split } from "@apollo/client";
import { WebSocketLink } from "@apollo/client/link/ws";
import { SubscriptionClient } from "subscriptions-transport-ws";
import { getMainDefinition } from "@apollo/client/utilities";
const client = new ApolloClient({
cache: new InMemoryCache()
});
export default function useClient(userId: string) {
const subscriptionClient = React.useRef<SubscriptionClient>(null);
React.useEffect(() => {
if (userId) {
if (subscriptionClient.current) {
subscriptionClient.current.close();
}
subscriptionClient.current = new SubscriptionClient(
"ws://localhost:3000/graphql",
{
reconnect: true,
connectionParams: { userId }
}
);
}
}, [userId]);
const splitLink = React.useMemo(() => {
const httpLink = new HttpLink({
uri: `http://localhost:3000//graphql`
});
if (userId && subscriptionClient.current) {
const websocketLink = new WebSocketLink(subscriptionClient.current);
return split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
websocketLink,
httpLink
);
}
return httpLink;
}, [userId]);
React.useEffect(() => {
client.setLink(splitLink);
}, [splitLink]);
return client;
} The main trick was to use |
Thanks for the contribution @vsylvestre! The state of subscription in the apollo ecosystem is plainly bad from the server to the client implementation. I can't wait for the subscriptions-transport-ws package to be removed. |
Hey @Sytten, maybe you can give Offering a completely different Protocol you'd have to use the same lib server side too; but, no stress - there are recipes in the readme for using it with Apollo both server and client side! |
how do you use this with HOC like next-apollo |
This method worked like a charm for my next.js project! |
Intended outcome:
As per the documentation of the function
Client.stop
, it should be ready to recycle the client once it is called. Meaning the websocket connection should be closed.Actual outcome:
Currently, the websocket connection is NOT closed. This is problematic since a very common use case if to recycle the client when the authorization token changes. If a user is not careful, he will end up creating a lot of websocket connections to the backend.
How to reproduce the issue:
The text was updated successfully, but these errors were encountered: