Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Integrating websocket link results in constat logging in console #1286

@1awaleed

Description

@1awaleed

I tried the first time to integrate WebSocketLink and it worked like a charm but as soon as I added WebSocketLink, the console keeps logging the same line every 1 minute. This is happening in a react-native project in a development build.

Expected Behavior
No logging.

Actual Behavior
The line being logged:
SocketRocket: In debug mode. Allowing connection to any root cert

A simple reproduction
How to reproduce:
The problem is occurring in a react native project, So to reproduce

  1. npx react-native init myApp
  2. Install all required dependencies used in the following piece of code.
  3. Create apolloInit.js.
import { ApolloClient } from 'apollo-client'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { setContext } from 'apollo-link-context'
import { split } from 'apollo-link'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'

// Create an http link:
const httpLink = new HttpLink({
  uri: 'https://localhost:3000/graphql'
});

const authLink = setContext((operation, { headers }) => {
  // get the authentication token from storage if it exists
  const token = await Asyncstorage.get('token')

  // return the headers to the context so httpLink can read them

  return {
    headers: {
      ...headers,
      authorization: token ? `Bearer ${token}` : '',
    },
  }
})

// Create a WebSocket link:
const token = await Asyncstorage.get('token')
const wsLink = new WebSocketLink({
  uri: `wss://localhost:5000/`,
  options: {
    reconnect: true,
    connectionParams: { authorization: token ? `Bearer ${token}` : '' }
  }
});

// using the ability to split links, you can send data to each link
// depending on what kind of operation is being sent
const link = split(
  // split based on operation type
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  wsLink,
  authLink.concat(httpLink),
);
  1. Modify app.js as follows:
import React from 'react'
import { View } from 'react-native'
import { ApolloProvider } from '@apollo/react-hooks'
import { store, persistor } from './src/redux/store/index'
import { client } from './src/services/apolloInitialization'

import { TestScreen } from './screens'

 <ApolloProvider client={client}>
   <View />
</ApolloProvider>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions