How to use the react-apollo.useSubscription function in react-apollo

To help you get started, we’ve selected a few react-apollo examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github michalkvasnicak / aws-lambda-graphql / packages / chat-example-app / src / components / Messages.tsx View on Github external
function Messages() {
  const [messages, setMessages] = useState([]);
  const onSubscriptionData = useCallback(
    (result: OnSubscriptionDataOptions) => {
      if (result.subscriptionData.data != null) {
        setMessages(state => [
          ...state,
          result.subscriptionData.data.messageFeed,
        ]);
      }
    },
    [],
  );
  const listRef = useRef(null);

  useSubscription(messageFeedSubscription, { onSubscriptionData });

  useEffect(() => {
    // scroll down
    if (listRef.current != null) {
      listRef.current.scrollTop = listRef.current.scrollHeight;
    }
  });

  return (
    
      {messages.map((message, i) => (
        
      ))}
    
  );
}
github Thorium-Sim / thorium / src / components / views / ProbeScience / probeScience.js View on Github external
const ProbeScienceSub = props => {
  const {simulatorId, emit, detect} = props;
  const {data = {}, loading} = useSubscription(PROBE_SCIENCE_EMITTER_SUB, {
    variables: {simulatorId},
  });
  const {scienceProbeEmitter: {name, type, charge} = {}} = data;
  React.useEffect(() => {
    if (!loading) {
      if (type === "burst") {
        emit(charge);
      } else {
        detect(charge, name);
      }
    }
  }, [loading, name, type, charge, emit, detect]);

  return null;
};
github OriginProtocol / origin / dapps / marketplace / src / pages / messaging / RefetchOnMessageData.js View on Github external
const RefetchOnMessageData = ({ refetch }) => {
  const callback = useCallback(() => {
    try {
      refetch()
    } catch (e) {
      console.error('failed to refetch', e)
    }
  })

  useSubscription(NewMessageSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MarkedAsReadSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MessagingStatusChangeSubscription, {
    onSubscriptionData: callback
  })

  return null
}
github OriginProtocol / origin / dapps / marketplace / src / pages / messaging / RefetchOnMessageData.js View on Github external
try {
      refetch()
    } catch (e) {
      console.error('failed to refetch', e)
    }
  })

  useSubscription(NewMessageSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MarkedAsReadSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MessagingStatusChangeSubscription, {
    onSubscriptionData: callback
  })

  return null
}
github OriginProtocol / origin / dapps / marketplace / src / pages / messaging / RefetchOnMessageData.js View on Github external
const RefetchOnMessageData = ({ refetch }) => {
  const callback = useCallback(() => {
    try {
      refetch()
    } catch (e) {
      console.error('failed to refetch', e)
    }
  })

  useSubscription(NewMessageSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MarkedAsReadSubscription, {
    onSubscriptionData: callback
  })

  useSubscription(MessagingStatusChangeSubscription, {
    onSubscriptionData: callback
  })

  return null
}
github OpenNeuroOrg / openneuro / packages / openneuro-app / src / scripts / datalad / subscriptions / useDraftSubscription.js View on Github external
const useDraftSubscription = (client, datasetId) => {
  useSubscription(DRAFT_SUBSCRIPTION, {
    variables: { datasetIds: [datasetId] },
    shouldResubscribe: true,
  })
  console.log('GET subscription update')
}
github OriginProtocol / origin / dapps / marketplace / src / components / EnableMessagingModal.js View on Github external
const EnableMessagingModal = () => {
  const [waitForSignature, setWaitForSign] = useState(false)

  const { data, error, networkStatus, refetch } = useQuery(query, {
    notifyOnNetworkStatusChange: true
  })

  useSubscription(MessagingStatusChangeSubscription, {
    onSubscriptionData: () => networkStatus !== 1 && refetch()
  })

  if (networkStatus === 1) {
    return 
  } else if (error) {
    return <p>Error :(</p>
  } else if (!data || !data.web3) {
    return <p>No Web3</p>
  }

  if (!data.messaging) {
    return 
  } else if (!data.messaging.synced) {
    return 
  } else if (!data.messaging.enabled &amp;&amp; !waitForSignature) {
github Thorium-Sim / thorium / src / components / views / CrmFighter / index.js View on Github external
const CrmData = props =&gt; {
  const {simulator} = props;
  const {data, loading} = useQuery(CRM_MOVEMENT_QUERY, {
    variables: {simulatorId: simulator.id},
  });
  const {data: subData} = useSubscription(CRM_MOVEMENT_SUBSCRIPTION, {
    variables: {simulatorId: simulator.id},
  });
  if (loading || !data) return null;
  const crm = subData ? subData.crmMovementUpdate : data.crm;
  return ;
};
github Thorium-Sim / thorium / src / components / views / Messaging / index.js View on Github external
() => ({
      variables: {simulatorId: simulator.id, station: station.name},
      updateQuery: (previousResult, {subscriptionData}) => {
        if (!subscriptionData.data.sendMessage) return previousResult;
        setTimeout(scrollElement, 100);
        return Object.assign({}, previousResult, {
          messages: previousResult.messages.concat(
            subscriptionData.data.sendMessage,
          ),
        });
      },
    }),
    [simulator.id, station.name],
  );
  useSubscribeToMore(subscribeToMore, MESSAGING_SUB, config);
  const {data: teamsSub} = useSubscription(MESSAGING_TEAMS_SUB, {
    variables: {simulatorId: simulator.id},
  });

  if (loading) return null;
  const {messages} = data;
  const teams = teamsSub ? teamsSub.teamsUpdate : data.teams;

  const messageGroups = station.messageGroups;
  const convoObj = messages
    .filter(
      m =>
        !teams.find(
          t =>
            t.cleared === true &&
            (t.name === m.sender || t.name === m.destination),
        ),