1

I am a subscription setup but onNext is not getting triggered I am not sure why since this is my first time implementing subscription and docs was not much help with the issue.

Here are the code implementations:

import {
  graphql,
  requestSubscription
} from 'react-relay'
import environment from '../network';

const subscription = graphql`
  subscription chatCreatedSubscription{
    chatCreated{
      id
      initiate_time
      update_time
      support_id
      category_id
      email
      name
    }
  }
  `;

function chatCreated(callback) {
  const variables = {};
  requestSubscription(environment, {
    subscription,
    variables,
    onNext: () => {
      console.log("onNext");
      callback()
    },
    updater: () => {
      console.log("updater");
    }
  });
}

module.exports = chatCreated;

and here is my network for the subscription

import { Environment, Network, RecordSource, Store } from "relay-runtime";
import Expo from "expo";
import { SubscriptionClient } from "subscriptions-transport-ws";
import { WebSocketLink } from 'apollo-link-ws';
import { execute } from 'apollo-link';
import accessHelper from "../helper/accessToken";

const networkSubscriptions = async (operation, variables) => {
  let token = await accessHelper();
  if (token != null || token != undefined) {
    const subscriptionClient = new SubscriptionClient("ws://localhost:3000/graphql",
      {
        reconnect: true,
        connectionParams: {
          Authorization: token,
        },
      });
    execute(new WebSocketLink(subscriptionClient), {
      query: operation.text,
      variables,
    });
  }
}

const network = Network.create(fetchQuery, networkSubscriptions);
const store = new Store(new RecordSource());

const environment = new Environment({
  network,
  store
});

export default environment;

the subscription is called in a componentDidMount method on a component it executes but the onNext method inside the subscription is never triggered when new information is added to what the subscription is listening to.

Paras Korat
  • 2,011
  • 2
  • 18
  • 36
ydvsailendar
  • 15
  • 10

1 Answers1

0

so i figured out that my issue was the network js not being setup properly and the version of subscription-transport-ws. i added version 0.8.3 of the package and made the following changes to my network file:

const networkSubscriptions = async (config, variables, cacheConfig, observer) => {
  const query = config.text;
  let token = await accessHelper();
  if (token != null || token != undefined) {
    const subscriptionClient = new SubscriptionClient(`ws://${api}/graphql`,
      {
        reconnect: true,
        connectionParams: {
          Authorization: token,
        },
      });
    subscriptionClient.subscribe({ query, variables }, (error, result) => {
      observer.onNext({ data: result })
    })
    return {
      dispose: subscriptionClient.unsubscribe
    };
  }
}

i hope this helps you if you get stuck with the same issue as mine.

ydvsailendar
  • 15
  • 10