apollographql / graphql-subscriptions

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

How to attach the pubsub.subscribe, unsubscribe function to apollo server? #179

Open hansololai opened 5 years ago

hansololai commented 5 years ago

I am using apollo server subscription, and I set up the subscription with the regular pubsub in this package. use the pubsub.asyncIterator for subscriptions, then use the same pubsub.publish(xxx).

pubsub also provide an subscribe and unsubscribe function which keep track of count/clients etc. But these two functions are not called if I just setup like this. I understand these are more for custome behavior and such. But my main goal is to know how many clients are subscribed to my channel, and the parameters when they subscribed, right before I call the pubsub.publish function. What is the best way to do it?

grantwwu commented 5 years ago

I'm not 100% sure what you're asking for, but pubsub.subscribe and pubsub.unsubscribe are mostly actually called internally by pubsub implementations in practice, inside the asyncIterator implementation.

I'm not a fan of the current PubSubEngine interface, to be frank, and this is one of the reasons - we force a specific interface for subscribe and unsubscribe when they're usually used internally.

https://github.com/axelspringer/graphql-google-pubsub/blob/master/src/async-iterator.ts#L106 is an example of it being called.

As for wanting statistics about # of clients subscribed and the parameters - that's out of scope of this repo and something you'd probably want to implement on your own as a GraphQL query that talks to your PubSub backing store.

hansololai commented 5 years ago

Thanks @grantwwu, yeah, I figured out how to do that my manually call pubsub.subscribe when user subscribe to it, and call pubsub.unsubscribe when user disconnect.

I downloaded the example usage of the async iterator, and I put breakpoint in the pubsub.subscribe function, it was never called. My point was, pubsub.subscribe/unsubscribe is not hooked to apollo server's onConnect/onDisconnect. So if I don't manually do stuff like

resolveer= {
Subscription:{
     userAdded: {
        subscribe:()=>{
           pubsub.subscribe("userAdded", some function)
           return pubsub.asyncIterator()
        }
    }
}

Then the subscriber is not counted. So if I use SubscriptionServer.connectionContext, I should get the number of subscribers, (not necessarily the graphql subscriber), but if I don't manually implement the above code, the pubsub.subscriber count is always 0. I don't think it's a bug, the subscribers are not really used anywhere. Just wanted to point out.

grantwwu commented 5 years ago

Which PubSubEngine implementation are you using? The example code you listed seems pretty wrong. The subscribe function actually allocates resources and makes a connection in many implementations, it's not something you do just to note that a new subscription has been made.

grantwwu commented 5 years ago

If you're referring to https://github.com/apollographql/graphql-subscriptions/blob/master/src/event-emitter-to-async-iterator.ts, yes, it does not use pubsub.subscribe. I don't know why. Accepting PRs to fix.