Skip to content

Commit a42ba35

Browse files
authored
Enable keepAlive in the graphql-ws client (#1916)
1 parent bc03d39 commit a42ba35

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/enable-keep-alive.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"ggt": patch
3+
---
4+
5+
Enable `keepAlive` in the graphql-ws client to keep the connection alive.
6+
7+
Users have pointed out that `ggt dev` stops receiving file updates sometimes after their computer has gone to sleep.
8+
9+
This fixes that issue by enabling the `keepAlive` option in the graphql-ws client. This makes the client send a ping every few seconds to the server to ensure the connection is still alive. Luckily for us, we've already implemented the pong handler on our servers because we use this same pattern in our editor!

src/services/app/client.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ExecutionResult } from "graphql-ws";
22
import { createClient } from "graphql-ws";
3+
import ms from "ms";
34
import type { ClientRequestArgs } from "node:http";
45
import PQueue from "p-queue";
56
import type { Promisable } from "type-fest";
@@ -41,6 +42,7 @@ export class Client {
4142

4243
this._graphqlWsClient = createClient({
4344
url: `wss://${environment.application.slug}.${config.domains.app}/edit/api/graphql-ws`, // FIXME: this assumes this is an Edit client
45+
keepAlive: ms("10s"),
4446
shouldRetry: () => true,
4547
connectionParams: {
4648
environment: environment.name,
@@ -59,6 +61,15 @@ export class Client {
5961
}
6062
},
6163
on: {
64+
opened: () => {
65+
this.ctx.log.trace("opened");
66+
},
67+
ping: (received) => {
68+
this.ctx.log.trace("ping", { received });
69+
},
70+
pong: (received) => {
71+
this.ctx.log.trace("pong", { received });
72+
},
6273
connecting: () => {
6374
switch (this.status) {
6475
case ConnectionStatus.DISCONNECTED:

vitest.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { timeoutMs } from "./spec/__support__/sleep.js";
33

44
export default defineConfig({
55
test: {
6-
include: ["**/spec/**/*.spec.ts"],
7-
exclude: [".direnv/**/*.spec.ts"],
6+
include: ["./spec/**/*.spec.ts"],
87
setupFiles: ["./spec/vitest.setup.ts"],
98
globalSetup: "./spec/vitest.global-setup.ts",
109
clearMocks: true, // so that we can `expect(someFunction).not.toHaveBeenCalled()` in tests where someFunction was called in another test

0 commit comments

Comments
 (0)