-
Notifications
You must be signed in to change notification settings - Fork 133
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
Handling client side unsubscribe #99
Comments
Same question. There's also a memory leak report in #97 from almost a month ago, but there's no signal from the team... |
I maybe misunderstanding the issue. The client's unsubscribe call includes the |
@NeoPhi Well, possibly - if there was a callback at the server side which would be called when a client would unsubscribe which would provide the |
Yes, the server implementation defines this hook: |
I don't get it how to implement this. Is there an example? In my case there are some watchers that need to be setup based on the subscription parameters. After the last client unsubscribes from a subscription I like to deconstruct the watcher to save some server resources. |
The connection status handled in subscription server code, not the grapqhl-subscriptions library.
.... later in your resolvers you can do:
|
This seems to be mostly resolved. If you want to learn more about the nitty-gritty of cleanup, #143 is a good place to start. I'm going to close this issue for now since it's been a while there was last any activity on it.. I'm going to leave conversation open, so feel free to respond if you want to pursue this further. |
the onCancel is called twice is that intended? |
When/where are you observing that behavior? |
what an important missing feature! |
PRs to make it easier to override the |
Using Karabur code worked for me using local graphql-subscriptions but started to fail once I started to use graphql-redis-subscriptions:
To make it work I had to change the withCancel function to this:
|
@leszekhanusz ran into the same issue with graphql-redis-subscriptions. Your solution still throws an error for me, I had to modify it in the following way to get it to work: export function withCancel<T>(
asyncIterator: AsyncIterator<T | undefined>,
onCancel: () => void
): AsyncIterator<T | undefined> {
if (!asyncIterator.return) {
asyncIterator.return = () => Promise.resolve({ value: undefined, done: true });
}
const savedReturn = asyncIterator.return.bind(asyncIterator);
asyncIterator.return = () => {
onCancel();
return savedReturn();
};
return asyncIterator;
} For some reason the this context of the asyncIterator needs to be preserved so I added: |
imo this ticket should not be closed,
|
@iambumblehead Totally agree. Current situation is a total mess |
When implementing the server side of GraphQL subscriptions, there is a callback called
subscribe
where you often delegate towithFilter
. This is called when the client subscribes and you provide afilterFn
that will be called on every emitted item.When the clients calls
unsubscribe
, it will sendGQL_STOP
and this should delete the subscription. How should the server implementor handle this? There doesn't seem to be aunsubscribe
callback. What actually happens when the client sendsGQL_STOP
is that thefilterFn
is called with thepayload
set to undefined (but this doesn't seem to be documented?)To prevent memory leaks, cleanup is necessary.
The text was updated successfully, but these errors were encountered: