From 9e55ea66fa5538eaafb372e6292e9de46c20d5d2 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 9 Jul 2019 16:27:58 +0000 Subject: [PATCH] [next] Websocket Keep Alive (#2394) * feat: added keepalive config * fix: format * fix: adjusted client timeout default * Update server.ts --- README.md | 3 +++ .../lib/network/createManagedSubscriptionClient.ts | 1 + src/core/server/config.ts | 8 ++++++++ src/core/server/graph/tenant/subscriptions/server.ts | 8 ++++++++ 4 files changed, 20 insertions(+) diff --git a/README.md b/README.md index dcffbbcb0..f675cef46 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,9 @@ the variables in a `.env` file in the root of the project in a simple - `CLUSTER_METRICS_PORT` - If `CONCURRENCY` is more than `1`, the metrics are provided at this port under `/cluster_metrics`. (Default `3001`) - `DISABLE_LIVE_UPDATES` - When `true`, disables subscriptions for the comment stream for all stories across all tenants (Default `false`) +- `WEBSOCKET_KEEP_ALIVE_TIMEOUT` - A duration in a parsable format (e.g. `30 seconds` + , `1 minute`) that should be used to send keep alive messages through the + websocket to keep the socket alive (Default `30 seconds`) ## License diff --git a/src/core/client/framework/lib/network/createManagedSubscriptionClient.ts b/src/core/client/framework/lib/network/createManagedSubscriptionClient.ts index e5635b08c..d24983217 100644 --- a/src/core/client/framework/lib/network/createManagedSubscriptionClient.ts +++ b/src/core/client/framework/lib/network/createManagedSubscriptionClient.ts @@ -89,6 +89,7 @@ export default function createManagedSubscriptionClient( if (!subscriptionClient) { subscriptionClient = new SubscriptionClient(url, { reconnect: true, + timeout: 60000, connectionCallback: err => { if (err) { // If an error is thrown as a result of live updates being diff --git a/src/core/server/config.ts b/src/core/server/config.ts index 2e673b1e2..0e8ea3b4d 100644 --- a/src/core/server/config.ts +++ b/src/core/server/config.ts @@ -173,6 +173,14 @@ const config = convict({ env: "STATIC_URI", arg: "staticUri", }, + websocket_keep_alive_timeout: { + doc: + "The keepalive timeout (in ms) that should be used to send keep alive messages through the websocket to keep the socket alive", + format: "duration", + default: "30 seconds", + env: "WEBSOCKET_KEEP_ALIVE_TIMEOUT", + arg: "websocketKeepAliveTimeout", + }, disable_tenant_caching: { doc: "Disables the tenant caching, all tenants will be loaded from MongoDB each time it's needed", diff --git a/src/core/server/graph/tenant/subscriptions/server.ts b/src/core/server/graph/tenant/subscriptions/server.ts index 2d84369d9..fbc392d87 100644 --- a/src/core/server/graph/tenant/subscriptions/server.ts +++ b/src/core/server/graph/tenant/subscriptions/server.ts @@ -223,6 +223,13 @@ export function createSubscriptionServer( schema: GraphQLSchema, options: Options ) { + const keepAlive = options.config.get("websocket_keep_alive_timeout"); + if (typeof keepAlive !== "number" || keepAlive <= 0) { + throw new Error( + "expected the websocket_keep_alive_timeout configuration to be a positive number" + ); + } + return SubscriptionServer.create( { schema, @@ -230,6 +237,7 @@ export function createSubscriptionServer( subscribe, onConnect: onConnect(options), onOperation: onOperation(options), + keepAlive, }, { server,