Hi everyone!
I’m currently working on setting up a subscription in React app using the Apollo 3 client and the useSubscription hook. I’m noticing that whenever I reload the page the subscription gets recreated successfully, but I’m also getting phantom updates over the socket from all previously created subscriptions that seem to still be open. Is this the expected behavior? Is there a way to check for existing subscriptions before recreating another? Not seeing anything in the docs.
Forgive my ignorance if this is something obvious, I’m new to GraphQL land.
reload the page several times after starting the dev server
look at the websocket connections in chrome dev tools and click on “graphql” to see the individual payloads being sent
post a message from the chat app and see that more than one payload with the same data gets received from the subscription.
reload the page again, than post another message from the chat app and see that now there are even more payloads being sent per post
The number of payloads being sent is consistent and only increases after the page is reloaded. I’m assuming that because the useSubscription hook is setting up a new subscription and the server isn’t checking that it already has an open subscription to that same client which is still active because it hasn’t timed out yet? I originally noticed the issue because each duplicate payload to the subscription was triggering a useEffect hook in my app to run multiple times per mutation. It also looks like each payload is costing a full query credit.
Attaching an image of what it looks like in my chrome dev tools when this happens.
Hi @dustin, the issue has been solved and merged to master. Actually we were polling for subscriptions and checking for terminations every 30 seconds. So that’s why we were getting the update for some time even after the subscription terminates. Now this duration is reduced to 2 seconds.
Hi @vvbalaji, we attach subscriptions to polling goroutines. After every fixed duration, we check is there any active subscription for the current poll. If not terminate this poll.
Actually recently we find out that we can still get duplicate updates if we don’t wait 2sec or 30sec(whatever the duration is) and start a new subscription. That’s also a bug and We will look into it and fix it soon.