Dealing with duplicate subscriptions

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.

Hey Dustin,

Could you provide me the steps to reproduce the issue. That would help us to debug it quickly.


Hi Balaji, I was just now able to reproduce the behavior with these steps

  1. clone the sample chat app following the instructions from here

  2. reload the page several times after starting the dev server

  3. look at the websocket connections in chrome dev tools and click on “graphql” to see the individual payloads being sent

  4. post a message from the chat app and see that more than one payload with the same data gets received from the subscription.

  5. 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.

Any luck reproducing this? Were my steps sufficient?

Hey. we found the issue. I think that issue has been fixed. @JatinDevDG can you confirm that?

1 Like

Hi, I am having a look into it.

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.

How do we check for terminations?

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.

1 Like