apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.59k stars 133 forks source link

Proposal: unsubscribe field #141

Open dyst5422 opened 6 years ago

dyst5422 commented 6 years ago

Currently there is a subscribe and a resolve field on resolvers.

I propose an unsubscribe field as well that can be used to release server resources.

There is currently no way to tell running processes that the subscription is no longer active to release the resources. An example is a memory heavy simulation system that is provisioned on subscribing and needs to be torn down when the client closes the connection.

Subscriptions: {
  mySub: {
    subscribe: () => AsyncIterator
    resolve: payload => resolvedPayload
    unsubscribe: () => void
  }
}

An implementation would have the server create a stack of unsubscribe functions on the socket connection that it adds to on each subscription call. When the socket it closed, it would call each of those unsubscribe functions.

grantwwu commented 5 years ago

The asyncIterator's return function is the intended way of cleaning up server resources, as I understand it. Unfortunately, it's not very reliable: https://github.com/apollographql/graphql-subscriptions/issues/143

This idea seems to be somewhat similar to what @taion did here: https://github.com/apollographql/graphql-subscriptions/pull/147#issuecomment-424861481

Definitely looking to figure out the right way forwards on this issue.

darrylsepeda commented 4 years ago

Is there any update with this enhancement?