2

Here's a piece of code I'm interested in (it's taken from /examples/ directory):

  Subscription: {
    counter: {
      subscribe: (parent, args, { pubsub }) => {
        const channel = Math.random().toString(36).substring(2, 15) // random channel name
        let count = 0
        // added var refreshIntervalId = 
        var refreshIntervalId = setInterval(() => pubsub.publish(channel, { counter: { count: count++ } }), 2000) // <----
        return pubsub.asyncIterator(channel)
      },
      // my new changes that hopefully will work
      onDisconnect: (webSocket, context) => {
          clearInterval(refreshIntervalId);
      } 
    }

I'm a bit concerned what's the best way (how can I pass refreshIntervalId between subscribe() and onDisconnect() to stop the interval once the connection is closed.

Update: I realized that I should insert onDisconnect under server's option (not in the resolver block), so I probably think I shouldn't really worried about it at all (it should handle disconnection by default).

A. Karnaval
  • 727
  • 2
  • 8
  • 12

0 Answers0