Skip to content
This repository was archived by the owner on Apr 14, 2023. It is now read-only.

Commit 0ce7a1e

Browse files
authored
add optional payload to ACK (#347)
* add optional payload to ACK * avoid race condition present in existing tests
1 parent 2a14451 commit 0ce7a1e

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
[@jedwards1211](https://github.com/jedwards1211) in [#675](https://github.com/apollographql/subscriptions-transport-ws/pull/675)
2020
- Accept extra WebSocket client arguments. <br/>
2121
[@GingerBear](https://github.com/GingerBear) in [#561](https://github.com/apollographql/subscriptions-transport-ws/pull/561)
22+
- Support server-defined payload in GQL_CONNECTION_ACK message. <br/>
23+
[@mattkrick](https://github.com/mattkrick) in [#347](https://github.com/apollographql/subscriptions-transport-ws/pull/347)
2224

2325
## v0.9.17
2426

Diff for: PROTOCOL.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ It server also respond with this message in case of a parsing errors of the mess
5151

5252
#### GQL_CONNECTION_ACK
5353
The server may responses with this message to the `GQL_CONNECTION_INIT` from client, indicates the server accepted the connection.
54+
May optionally include a payload.
5455

5556

5657
#### GQL_DATA

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,12 @@ ReactDOM.render(
263263
- => Returns an `off` method to cancel the event subscription.
264264

265265
#### `onConnected(callback, thisContext) => Function` - shorthand for `.on('connected', ...)`
266-
- `callback: Function`: function to be called when websocket connects and initialized, after ACK message returned from the server
266+
- `callback: Function(payload)`: function to be called when websocket connects and initialized, after ACK message returned from the server. Includes payload from server, if any.
267267
- `thisContext: any`: `this` context to use when calling the callback function.
268268
- => Returns an `off` method to cancel the event subscription.
269269

270270
#### `onReconnected(callback, thisContext) => Function` - shorthand for `.on('reconnected', ...)`
271-
- `callback: Function`: function to be called when websocket reconnects and initialized, after ACK message returned from the server
271+
- `callback: Function(payload)`: function to be called when websocket reconnects and initialized, after ACK message returned from the server. Includes payload from server, if any.
272272
- `thisContext: any`: `this` context to use when calling the callback function.
273273
- => Returns an `off` method to cancel the event subscription.
274274

Diff for: src/client.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ export class SubscriptionClient {
624624
break;
625625

626626
case MessageTypes.GQL_CONNECTION_ACK:
627-
this.eventEmitter.emit(this.reconnecting ? 'reconnected' : 'connected');
627+
this.eventEmitter.emit(this.reconnecting ? 'reconnected' : 'connected', parsedMessage.payload);
628628
this.reconnecting = false;
629629
this.backoff.reset();
630630
this.maxConnectTimeGenerator.reset();

Diff for: src/test/tests.ts

+39
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const ONCONNECT_ERROR_TEST_PORT = TEST_PORT + 6;
4545
const ERROR_TEST_PORT = TEST_PORT + 7;
4646

4747
const SERVER_EXECUTOR_TESTS_PORT = ERROR_TEST_PORT + 8;
48+
const ACK_ONCONNECTED_TEST_PORT = TEST_PORT + 8;
49+
const ACK_ONRECONNECTED_TEST_PORT = TEST_PORT + 9;
4850

4951
const data: { [key: string]: { [key: string]: string } } = {
5052
'1': {
@@ -732,6 +734,43 @@ describe('Client', function () {
732734
});
733735
});
734736

737+
it('should handle correctly GQL_CONNECTION_ACK payload for onConnected', (done) => {
738+
const server = createServer(notFoundRequestListener);
739+
server.listen(ACK_ONCONNECTED_TEST_PORT);
740+
const wss = new WebSocket.Server({server});
741+
wss.on('connection', (connection: any) => {
742+
connection.on('message', (message: any) => {
743+
connection.send(JSON.stringify({ type: MessageTypes.GQL_CONNECTION_ACK, payload: {appVersion: '1.0.0'} }));
744+
wss.close();
745+
});
746+
});
747+
748+
const client = new SubscriptionClient(`ws://localhost:${ACK_ONCONNECTED_TEST_PORT}/`);
749+
client.onConnected((payload) => {
750+
expect(payload.appVersion).to.equal('1.0.0');
751+
done();
752+
});
753+
});
754+
755+
it('should handle correctly GQL_CONNECTION_ACK payload for onReconnected', (done) => {
756+
const server = createServer(notFoundRequestListener);
757+
server.listen(ACK_ONRECONNECTED_TEST_PORT);
758+
const wss = new WebSocket.Server({server});
759+
wss.on('connection', (connection: any) => {
760+
connection.on('message', (message: any) => {
761+
connection.send(JSON.stringify({ type: MessageTypes.GQL_CONNECTION_ACK, payload: {appVersion: '1.0.0'} }));
762+
wss.close();
763+
});
764+
});
765+
766+
const client = new SubscriptionClient(`ws://localhost:${ACK_ONRECONNECTED_TEST_PORT}/`, { reconnect: true });
767+
client.close(false, false);
768+
client.onReconnected((payload) => {
769+
expect(payload.appVersion).to.equal('1.0.0');
770+
done();
771+
});
772+
});
773+
735774
it('removes subscription when it unsubscribes from it', function () {
736775
const client = new SubscriptionClient(`ws://localhost:${TEST_PORT}/`);
737776

0 commit comments

Comments
 (0)