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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With