Skip to content

How to unsubscribe before resubscribing when subscription variables change? #385

Answered by enisdenjo
jacobhoekert asked this question in Q&A
Discussion options

You must be logged in to vote

You're probably recreating the client every time the useEffect hook gets called. In graphql-ws 1 client means 1 socket, a single client will never have multiple open sockets.

I always recommend splitting the hooks, try something like this:

const client = useMemo(
  () =>
    createClient({
      url: 'GRAPH_QL_URL',
      connectionParams() {
        return {
          headers: {
            key: 'token',
          },
        }
      },
    }),
  [],
)

useEffect(() => {
  const onNext = (data) => {
    // Handling onNext events
  }
  const onError = (err) => {
    // Handle errors
  }
  const onComplete = () => {
    // Subscription completed
  }

  return client.subscribe(
    {
      v…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by enisdenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants