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

Prevent messages from being sent when the client is closing. #760

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

### v0.9.17
- Prevent messages being sent when the client is closing. <br/>

### v0.9.16
- Add ability to set custom WebSocket protocols for client. <br/>
[@pkosiec](https://github.com/pkosiec) in [#477](https://github.com/apollographql/subscriptions-transport-ws/pull/477)
Expand Down
6 changes: 5 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,13 @@ export class SubscriptionClient {
}

// send message, or queue it if connection is not open
private sendMessageRaw(message: Object) {
private sendMessageRaw(message: any) {
switch (this.status) {
case this.wsImpl.OPEN:
if(message.type === MessageTypes.GQL_STOP) {
this.eventEmitter.emit('error', new Error(`Client is closing. Preventing message to be sent: ${message.type}`));
break;
}
let serializedMessage: string = JSON.stringify(message);
try {
JSON.parse(serializedMessage);
Expand Down
13 changes: 13 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,19 @@ describe('Client', function () {
done();
});

it('should throw if client is being closed', (done) => {
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`);
const originalOnMessage = subscriptionsClient.client.onmessage;
const dataToSend = {
data: JSON.stringify({ type: 'stop' }),
};

expect(() => {
originalOnMessage.call(subscriptionsClient, dataToSend)();
}).to.throw('Client is closing. Preventing message to be sent: stop');
done();
});

it('should delete operation when receive a GQL_COMPLETE', (done) => {
const subscriptionsClient = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`);
subscriptionsClient.operations['1'] = {
Expand Down