graphql-subscriptions
graphql-subscriptions copied to clipboard
Find no. of clients connected to a subscription
I'm building a chat widget where the users will be subscribed to the following subscription. For each user, the 'user_id' will be different
subscription {
newUserMessage(user_id: 123) {
id
content
type
}
}
I want to find whether a user is online or not. So is there any way to find the number of clients connected to a particular subscription?
My subscription's resolver looks like this:
newUserMessage: {
subscribe: withFilter(
() => pubsub.asyncIterator("newUserMessage"),
({ newUserMessage }, args) => {
return args.user_id === newUserMessage.user_id;
}
)
}
I don't believe we have any built-in support for this, although it's something we could add. However cleanup of resources is a bit unreliable right now... see https://github.com/apollographql/graphql-subscriptions/issues/143 - so detecting when a client connection is closed might be hard.
is there any update on this topic??
@gijo-varghese do you recall what solution you ended up using? We have a similar use-case.
Another approach could also be to listen/have a callback in the backend for when the connection is closed for any given user, and then use redis to notify the other users that the given client is disconnected. But @grantwwu you mentioned that might be difficult?