4

I need to integrate subscription in my ReactNative app. The subscription works fine on localhost in graphiql. I have deployed my backend on Heroku. I am using apollo-server and not hasura. My subscriptions are not working for the url given by Heroku but it works fine on localhost. Queries and mutations work fine for both localhost and Heroku url. So I am trying to access my subscription from my ReactNative client. I have kept the base url as my local host. The queries and mutations part works for my ReactNative client but my subscription part is not working.

I have configured my Apollo client for subscription by adding this

const httpLink = createHttpLink({
  uri: 'http://localhost:5000',
});

const wsLink = new WebSocketLink({
  uri: `ws://localhost:5000`,
  options: {
    reconnect: true,
    connectionParams: {
      // headers: {
      //   Authorization: `Bearer ${token}`,
      // },
    },
  },
});

const authLink = setContext(async (req, {headers}) => {
  try {
    const token = await AsyncStorage.getItem('token');
    return {
      headers: {
        ...headers,
        authorization: token ? `Bearer ${token}` : '',
      },
    };
  } catch (e) {
    console.log(e);
  }
});

const link = split(
  ({query}) => {
    const {kind, operation} = getMainDefinition(query);
    return kind === 'OperationDefinition' && operation === 'subscription';
  },
  wsLink,
  authLink.concat(httpLink),
);

const client = new ApolloClient({
  link: link,
  cache: new InMemoryCache(),
});

Here is my useSubscription hook

const {data, error, loading} = useSubscription(
    HEALTH_CONSULTATION_SUBSCRIPTION,
  );

I am neither getting error nor data. I am triggering the subscription from Graphiql

5
  • is the url you are triyng to connect wss or https? If it is a domain and not a localhost you have to add a secure transport exception for ios and android:stackoverflow.com/questions/30731785/… Commented Jun 16, 2020 at 10:37
  • @DanielDimitrov I have added it for iOS in info plist file. I have tried on https as well as localhost but does not work. I tried replacing https with ws , still it won't work. Also its ws and not was. Also my mutation and query are working so its not a transport issue Commented Jun 16, 2020 at 14:56
  • So what is the exact error you get in the console? Commented Jun 17, 2020 at 4:33
  • @DanielDimitrov I don't see anything no failure no success Commented Jun 17, 2020 at 9:27
  • Where are you looking? Do you see the request dispatching in the network inspect? Do you get a response? Maybe remove the try/catch block and see if you'll get an error. Commented Jun 17, 2020 at 9:46

1 Answer 1

2

So there was no problem from my React Native Code. The issue was with the free tier of Heroku I am using. I tried replacing my subscription with Hasura's Subscription https://hasura.io/learn/graphql/graphiql?tutorial=react-native and it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.