4

I'm developing a real-time chat app using Apollo GraphQL, and I want to keep the clients updated about the other client's status.

Mean that I need to do something with the onConnect and OnDisconnect events from the subscriptions object (which defined in app.js) and send a subscription event to the clients whenever a user connected or disconnected.

I could not find an explanation on how to do this, and I would appreciate any help.

Subrato Pattanaik
  • 5,331
  • 6
  • 21
  • 52
Guy Ben David
  • 478
  • 3
  • 15

1 Answers1

2

you have to use onDisconnect property with context.initPromise like this:

    onDisconnect: async (webSocket, context) => {
      console.log('what is the context?: ', context); 

      try {
        const initialContext = await context.initPromise;
        if (
          initialContext &&
          typeof initialContext === 'object' &&
          Reflect.has(initialContext, 'user')
        ) {
          console.log('initialContext: ', initialContext);
        }
      } catch (err) {
        console.log('error', err); // TypeError: 
      }
    }
  },

link and link2 for reference.

warning: this is currently working on my local build but not on my heroku production build

loekTheDreamer
  • 576
  • 7
  • 24