Skip to content

Commit

Permalink
feat(client): disablePong option for when implementing a custom pinger
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Jun 9, 2021
1 parent 2fe0345 commit 6510360
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -648,6 +648,7 @@ function createPingerClient(options: ClientOptions): PingerClient {
let activeSocket: WebSocket;

const client = createClient({
disablePong: true,
...options,
on: {
connected: (socket) => {
Expand Down
13 changes: 13 additions & 0 deletions docs/interfaces/client.clientoptions.md
Expand Up @@ -11,6 +11,7 @@ Configuration used for the GraphQL over WebSocket client.
### Properties

- [connectionParams](client.clientoptions.md#connectionparams)
- [disablePong](client.clientoptions.md#disablepong)
- [generateID](client.clientoptions.md#generateid)
- [isFatalConnectionProblem](client.clientoptions.md#isfatalconnectionproblem)
- [jsonMessageReplacer](client.clientoptions.md#jsonmessagereplacer)
Expand Down Expand Up @@ -43,6 +44,18 @@ in the close event reason.

___

### disablePong

`Optional` **disablePong**: `boolean`

Disable sending the `PongMessage` automatically.

Useful for when integrating your own custom client pinger that performs
custom actions before responding to a ping, or to pass along the optional pong
message payload. Please check the readme recipes for a concrete example.

___

### generateID

`Optional` **generateID**: () => `string`
Expand Down
15 changes: 13 additions & 2 deletions src/client.ts
Expand Up @@ -247,6 +247,14 @@ export interface ClientOptions {
* @default 0
*/
keepAlive?: number;
/**
* Disable sending the `PongMessage` automatically.
*
* Useful for when integrating your own custom client pinger that performs
* custom actions before responding to a ping, or to pass along the optional pong
* message payload. Please check the readme recipes for a concrete example.
*/
disablePong?: boolean;
/**
* How many times should the client try to reconnect on abnormal socket closure before it errors out?
*
Expand Down Expand Up @@ -353,6 +361,7 @@ export function createClient(options: ClientOptions): Client {
onNonLazyError = console.error,
lazyCloseTimeout = 0,
keepAlive = 0,
disablePong,
retryAttempts = 5,
retryWait = async function randomisedExponentialBackoff(retries) {
let retryDelay = 1000; // start with 1s delay
Expand Down Expand Up @@ -546,11 +555,13 @@ export function createClient(options: ClientOptions): Client {
emitter.emit('message', message);
if (message.type === 'ping' || message.type === 'pong') {
emitter.emit(message.type, true, message.payload); // received
if (message.type === 'ping') {
if (message.type === 'pong') {
enqueuePing(); // enqueue next ping (noop if disabled)
} else if (!disablePong) {
// respond with pong on ping
socket.send(stringifyMessage({ type: MessageType.Pong }));
emitter.emit('pong', false, undefined);
} else enqueuePing(); // enqueue next ping on pong (noop if disabled)
}
return; // ping and pongs can be received whenever
}
if (acknowledged) return; // already connected and acknowledged
Expand Down
26 changes: 26 additions & 0 deletions src/tests/client.ts
Expand Up @@ -429,6 +429,32 @@ describe('ping/pong', () => {
}, 20);
});

it('should not respond with a pong to a ping when disabled', async () => {
const { url, waitForConnect, waitForClient, waitForClientClose } =
await startTServer();

createClient({
url,
lazy: false,
retryAttempts: 0,
onNonLazyError: noop,
disablePong: true,
});

await waitForConnect();

await waitForClient((client) => {
client.send(stringifyMessage({ type: MessageType.Ping }));
client.onMessage(() => {
fail("Shouldn't have received a message");
});
});

await waitForClientClose(() => {
fail("Shouldn't have closed");
}, 20);
});

it('should not react to a pong', async () => {
const { url, waitForConnect, waitForClient, waitForClientClose } =
await startTServer();
Expand Down

0 comments on commit 6510360

Please sign in to comment.