title: How to close socket connection for GraphQL subscription in Apollo
body: <p>I have GraphQL Subscriptions on my Apollo server that I want to close after the user logs out. The initial question is whether we should close this (socket) connections on the client side or in the backend.</p>
  <p>On the front-end, I am using Angular with Apollo Client and I handle GraphQL subscriptions by extending the <code>Subscription</code> class from <code>apollo-angular</code>. I am able to close the subscription channels with a typical <code>takeUntil</code> rxjs implementation:</p>
  <pre><code>this.userSubscription
    .subscribe()
    .pipe(takeUntil(this.subscriptionDestroyed$))
    .subscribe(
      ({ data }) =&gt; {
        // logic goes here
      },
      (error) =&gt; {
        // error handling
     }
  );
    
  </code></pre>
  <p>However, this does not close the websocket on the server, which If I'm right, will result in a subscription memory leak.</p>
  <p>The way the Apollo Server (and express) is set up for subscriptions is as follows:</p>
  <pre><code>const server = new ApolloServer({
    typeDefs,
    resolvers,
    subscriptions: {
      onConnect: (connectionParams, webSocket, context) =&gt; {
        console.log('on connect');
        const payload = getAuthPayload(connectionParams.accessToken);
        if (payload instanceof Error) {
          webSocket.close();
        }
        return { user: payload };
      },
      onDisconnect: (webSocket, context) =&gt; {
        console.log('on Disconnect');
      }
    },
    context: ({ req, res, connection }) =&gt; {
      if (connection) {
        // set up context for subscriptions...
      } else {
        // set up context for Queries, Mutations...
      }
  </code></pre>
  <p>When the client registers a new GraphQL subscription, I always get to see <code>console.log('on connect');</code> on the server logs, but I never see <code>console.log('on Disconnect');</code> unless I close the front-end application.</p>
  <p>I haven't seen any example on how to close the websocket for subscriptions with Apollo. I mainly want to do this to complete a Logout implementation.</p>
  <p>Am I missing something here? Thanks in advance!</p>