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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With