Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,19 @@ describe('GraphQL subscriptions', () => {
subscription.unsubscribe();
});

it('replies with a pong when the server sends a ping', async () => {
const subscription = fixture.triggerSubscription();

await fixture.handleConnectionInit();
await fixture.consumeSubscribeMessage();

fixture.sendMessageToClient({ type: 'ping' });

expect(await fixture.getNextMessage()).toEqual({ type: 'pong' });

subscription.unsubscribe();
});

it('when the server sends an error, it will reconnect and subscribe again', async () => {
const subscription = fixture.triggerSubscription();
useFakeSetInterval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe('LegacySubscriptionProtocol', () => {
it('serializes ping without id and payload', () => {
expect(JSON.parse(protocol.serializePing())).toEqual({ type: 'ping' });
});

it('serializes pong without id and payload', () => {
expect(JSON.parse(protocol.serializePong())).toEqual({ type: 'pong' });
});
});

describe('incoming frames', () => {
Expand All @@ -47,6 +51,12 @@ describe('LegacySubscriptionProtocol', () => {
).toEqual({ type: 'connection-ack' });
});

it('parses a server-sent ping', () => {
expect(protocol.parseIncomingMessage('{"type":"ping"}')).toEqual({
type: 'ping',
});
});

it('parses pong', () => {
expect(protocol.parseIncomingMessage('{"type":"pong"}')).toEqual({
type: 'pong',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ export class GraphqlService {
break;
}

case 'ping':
this.sendRawMessage(this.protocol.serializePong());
break;

case 'pong':
clearTimeout(this.pongTimeout);
this.connectionAttemptsCount = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface QminderGraphQLError {
export type IncomingSubscriptionMessage =
| { readonly type: 'connection-ack' }
| { readonly type: 'keep-alive' }
| { readonly type: 'ping' }
| { readonly type: 'pong' }
| {
readonly type: 'data';
Expand Down Expand Up @@ -45,6 +46,7 @@ export interface SubscriptionProtocol {
serializeSubscribe(id: string, query: string): string;
serializeUnsubscribe(id: string): string;
serializePing(): string;
serializePong(): string;
parseIncomingMessage(data: string): IncomingSubscriptionMessage;
}

Expand Down Expand Up @@ -106,6 +108,10 @@ export class LegacySubscriptionProtocol implements SubscriptionProtocol {
return JSON.stringify({ type: LegacyMessageType.GQL_PING });
}

serializePong(): string {
return JSON.stringify({ type: LegacyMessageType.GQL_PONG });
}

parseIncomingMessage(data: string): IncomingSubscriptionMessage {
const message: LegacyMessage = JSON.parse(data);

Expand All @@ -116,6 +122,9 @@ export class LegacySubscriptionProtocol implements SubscriptionProtocol {
case LegacyMessageType.GQL_CONNECTION_ACK:
return { type: 'connection-ack' };

case LegacyMessageType.GQL_PING:
return { type: 'ping' };

case LegacyMessageType.GQL_PONG:
return { type: 'pong' };

Expand Down
Loading