Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with using keepAlive if set for 20 seconds or more

Having a bit of trouble with keepAlive for Apollo subscriptions. When ever I set a time to seconds or more the listening subscriptions errors out.

{
  "error": "Could not connect to websocket endpoint ws://website.test:8000/graphql. Please check if the endpoint url is correct."
}

Here is ApolloServer setup

const apollo = new ApolloServer({
    introspection: true,
    playground: true,
    typeDefs: schema,
    subscriptions: {
      keepAlive: 40000,
    },
    resolvers,
    context: ........
}

In my local environment when I do not set keepAlive it will stay open indefinitely. If I set it at 10000 works great. With keep alive set at 40000 I get the error and connection closes

UPDATE One thing we just noticed is that this issue happens on the playground but not on our web app. Maybe just a playground thing?

like image 845
Daniel Dodd Avatar asked Aug 30 '19 20:08

Daniel Dodd


People also ask

What is keep alive interval?

Keepalive interval is the duration between two successive keepalive retransmissions, if acknowledgement to the previous keepalive transmission is not received. Keepalive retry is the number of retransmissions to be carried out before declaring that remote end is not available.

What is the purpose of the keepalive message?

Keepalive can tell you when another peer becomes unreachable without the risk of false-positives. In fact, if the problem is in the network between two peers, the keepalive action is to wait some time and then retry, sending the keepalive packet before marking the connection as broken.

How do I set up keepalive?

To enable Keep-Alive, you need to explicitly request it via the HTTP header by accessing . htaccess or the main configuration file of your web server. If you turn on Keep-Alive, the HTTP response header will show Connection: keep-alive.

What is Apollo server GraphQL?

Apollo Server is an open-source, spec-compliant GraphQL server that's compatible with any GraphQL client, including Apollo Client. It's the best way to build a production-ready, self-documenting GraphQL API that can use data from any source.


1 Answers

If you check the documentation provided by Apollo GraphQL:

keepAlive - Number

The frequency with which the subscription endpoint should send keep-alive messages to open client connections, in milliseconds, if any.

If this value isn't provided, the subscription endpoint doesn't send keep-alive messages.

As per your provided configuration, with keep alive set at 40000, this is equivalent to 40 seconds. So the subscription endpoint is pinging keep-alive messages every 40 seconds. This probably is too long and the connections is already closed.

You should use a smaller value

like image 52
Julius A Avatar answered Oct 21 '22 15:10

Julius A