useSubscription Socket errors being thrown in the console

useSubscription Socket errors being thrown in the console


1

I hope you are doing well!

We are using web sockets in our website with graphQL hence we are using useSubscription.

It is working fine, I get the data back when I need it; however, we are facing a small issue that while it is not stopping our website from functioning, it is extremely weird and awkward for users using the website.

Basically, randomly, without any call from the backend web socket the useSubscription sometimes throws multiple errors in the console at no specific time, very randomly.

There are 2 errors:

The first one is:

WebSocket connection to URL failed: Insufficient resources.

The second one is:

WebSocket connection to URL failed: WebSocket is closed before the connection is established.

With URL being replaced with our backend WebSocket URL.

We know that the URL is valid since it works perfectly fine, and we get data when needed. However, this error is weird since we get it at random times without any call from the backend web socket.

We are using GraphQL and @apollo/client to handle it. And we are getting our SubscriptionClient from subscriptions-transport-ws.

Here is our code for the initialization of the client

import { ApolloClient, InMemoryCache, split } from "@apollo/client";
import { getMainDefinition } from "@apollo/client/utilities";
import { HttpLink } from "apollo-link-http";
import { WebSocketLink } from '@apollo/client/link/ws';
import { SubscriptionClient } from 'subscriptions-transport-ws';
import { link, socketLink } from "./gql-link";

const httpLink = new HttpLink({
  uri: link,
});

const subscriptionClient = new SubscriptionClient(socketLink, {
  reconnect: true,
});

const wsLink = new WebSocketLink(subscriptionClient);

const splitLink = split(
  ({ query }) => {
    const { kind, operation } = getMainDefinition(query);

    return kind === "OperationDefinition" && operation === "subscription";
  },
  wsLink,
  httpLink
);

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

export default client;


Thank You a lot for your help in advanced!

New contributor

Maya K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *