So I've got NestJs application, where I've gotmultiple subscriptions. from client side on my react app with apolloClinet I'm listening one continuously that's notification Listener.
I also have messaging subscription that fetches new messages when I'm in chat. I want my setup to be set like when user open chatRoom to add in listeners in db, and when he leaves fetchNewMessages subscription to delete from chatRoom Listeners
so I did this on my subscription resolver that is adding currentUser in chatRoom Listener list
fetchNewMessage(@CurrentUser('') user: User, @Args('chatRoomId') chatRoomId: string) {
this.messagesService.addRemoveListenerOnChatRoom(chatRoomId, user.id, 'add')
return PubSub.asyncIterator('newMessage')
}
but when I'm leaving chatRoom I don't have access onDisconnect property to remove them from listeners
I only have main application graphql-ws onDisconnect method on app
subscriptions: {
'graphql-ws': {
onConnect: context => {
const { connectionParams, extra } = context
const authToken = connectionParams.Authorization && connectionParams.Authorization.replace('Bearer ', '')
const user = authService.getJwtPayLoad(authToken)
// when using with graphql-ws, additional context value should be stored in the extra field
extra.headers = { user, authorization: connectionParams.Authorization }
},
onDisconnect: (websocket, context) => {
const user = context.extra.user
user && messagesService.addRemoveListenerOnChatRoom(null, user.id, 'remove')
},
},
context: ({ extra }) => {
return extra
// you can now access your additional context value through the extra field
},
but this isn't useful cause I'm always listening to notification subscription so if I leave fetchNewChatRoom listener this onDisconnect will not work
so is there any suggest or best practices on multi subscription servers ? can I adjust properties on individual subscription onDisconnect ?