apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.59k stars 133 forks source link

Find no. of clients connected to a subscription #128

Open gijo-varghese opened 6 years ago

gijo-varghese commented 6 years ago

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;
      }
    )
  }
grantwwu commented 5 years ago

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.

aramisromero89 commented 3 years ago

is there any update on this topic??

simplenotezy commented 1 year ago

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