Our team is mostly using “normal” request operations in Apollo, but are starting to use subscriptions for some long-running tasks (at the moment file-export).
Therefore a discussion started in our team; is it somehow possible for the client to cancel a subscription - so the apollo server can abort and clean up any expensive operations?
This could for example be the case if a user would initiate a file download, and then cancel?
@alexb the useSubscription hook will automatically unsubscribe any started subscriptions, when the component using it is unmounted. If you want manual control over when subscriptions are stopped, you’re better off using Apollo Client’s client.subscribe() method directly, like:
const observer = client.subscribe({
query: SOME_SUBSCRIPTION,
variables: {
someId,
},
});
const subscription = observer.subscribe(({ data }) => {
console.log('data received', data);
...
});
...
// When you want to unsubscribe yourself, call
subscription.unsubscribe();
Good point (we are using svelte, but same principles apply). I’m more thinking about the server here. How can we notify the server that the subscription was cancelled?
@michael-watson correct me if I’m wrong, but is onDisconnect not when the socket itself is closed, and not when an individual subscription is cancelled?
Yes but I need to double check, I thought it would still fire for the cancel but I’m not sure if there is a way to tell that the closed socket is the cancelled one