graphql-subscriptions icon indicating copy to clipboard operation
graphql-subscriptions copied to clipboard

Find no. of clients connected to a subscription

Open gijo-varghese opened this issue 7 years ago • 3 comments

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;
      }
    )
  }

gijo-varghese avatar Jan 14 '18 04:01 gijo-varghese

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.

grantwwu avatar Sep 26 '18 23:09 grantwwu

is there any update on this topic??

aramisromero89 avatar Sep 10 '21 17:09 aramisromero89

@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?

simplenotezy avatar May 10 '23 15:05 simplenotezy