Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: Subscriptions don't close and don't receive data #95

Open
stramel opened this issue Mar 26, 2020 · 4 comments
Open

[Question]: Subscriptions don't close and don't receive data #95

stramel opened this issue Mar 26, 2020 · 4 comments

Comments

@stramel
Copy link

stramel commented Mar 26, 2020

I'm running into issues using subscriptions-transport-ws with this. The subscriptions never close and don't seem to be getting data.

import { SubscriptionClient } from 'subscriptions-transport-ws'

// ...

const client = new SubscriptionClient('/api/graphql', {
  reconnect: true
})

function getSubscribeFn(client) {
  return (operation, variables) => {
    return client.request({ query: operation.text, variables, operationName: operation.name })
  }
}


// ...

{
  subscribeFn: getSubscribeFn(client)
  }
}

It would be nice to export a subscribeFn to properly handle subscriptions.
It could easily just accept a SubscriptionClient instance.

@stramel
Copy link
Author

stramel commented Mar 26, 2020

This doesn't seem like the correct solution but it is now properly closing subscriptions. Can someone confirm or provide a correct solution?

{
  subscribeFn: (operation, variables) => {
    return Observable.create(sink =>
      // @ts-ignore
      client.request({ query: operation.text, variables, operationName: operation.name }).subscribe(sink)
    )
  }
}

@AnotherHermit
Copy link
Collaborator

AnotherHermit commented Mar 26, 2020

This is the solution we're using, I clipped out our authentication and error handling. I haven't used Observables enough to say if it is equivalent to the solution you are using.

const client = new SubscriptionClient(wsUrl, { lazy: true, reconnect: true });

const subscribeFn = (operation, variables) => {
  const query = operation.text;
  if (!query) return;
  const request = client.request({ query, variables });
  return {
    subscribe: observer => {
      const subscription = request.subscribe(observer);
      observer.start(subscription);
    },
  };
};

@stramel
Copy link
Author

stramel commented Mar 26, 2020

@AnotherHermit Thanks for the example of how you're handling it. It worked for me as well.

I did have some issues with the typings that said that dispose was required. Is there any interest in offering a minimal function that accepts a SubscriptionClient and returning a subscribeFn so users don't have to worry about this stuff?

This is basically what I'm thinking about.

function getSubscribeFn(client: SubscriptionClient) {
  return (operation: ConcreteBatch, variables: Variables) => {
    const query = operation.text
    if (!query) return

    const request = client.request({
      query,
      variables,
      operationName: operation.name,
    })

    return {
      subscribe: observer => {
        const subscription = request.subscribe(observer)
        observer.start(subscription)
      },
      dispose: () => {
        client.unsubscribeAll()
        client.close()
      },
    }
  }
}

@AlicanC
Copy link
Contributor

AlicanC commented May 27, 2020

I've followed an article and ended up with this:

import { execute } from 'apollo-link';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { WebSocketLink } from 'apollo-link-ws';

const subscriptionClient = new SubscriptionClient('ws://localhost:4000/graphql', {
  lazy: true,
  reconnect: true,
});

const webSocketLink = new WebSocketLink(subscriptionClient);

const subscribeFn = (operation, variables) =>
  execute(webSocketLink, {
    query: operation.text,
    variables,
  });

Haven't deployed to production yet, but it seems to work well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants