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

Commit

Permalink
Merge pull request #85 from timsuchanek/patch-3
Browse files Browse the repository at this point in the history
Send INIT before SUBSCRIPTION_START
  • Loading branch information
NeoPhi authored Mar 3, 2017
2 parents 39caa39 + 1017c4b commit 7dba040
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ export class SubscriptionClient {
this.eventEmitter.emit(isReconnect ? 'reconnect' : 'connect');
this.reconnecting = false;
this.backoff.reset();
// Send INIT message, no need to wait for connection to success (reduce roundtrips)
this.sendMessage({type: INIT, payload: this.connectionParams});

Object.keys(this.reconnectSubscriptions).forEach((key) => {
const { options, handler } = this.reconnectSubscriptions[key];
this.subscribe(options, handler);
Expand All @@ -251,9 +254,6 @@ export class SubscriptionClient {
this.client.send(JSON.stringify(message));
});
this.unsentMessagesQueue = [];

// Send INIT message, no need to wait for connection to success (reduce roundtrips)
this.sendMessage({type: INIT, payload: this.connectionParams});
};

this.client.onclose = () => {
Expand Down
37 changes: 34 additions & 3 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,40 @@ describe('Client', function () {
new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`);
});

it('should send INIT message first, then the SUBSCRIPTION_START message', (done) => {
let initReceived = false;

const client = new SubscriptionClient(`ws://localhost:${RAW_TEST_PORT}/`);
wsServer.on('connection', (connection: any) => {
connection.on('message', (message: any) => {
const parsedMessage = JSON.parse(message);
// mock server
if (parsedMessage.type === INIT) {
connection.send(JSON.stringify({type: INIT_SUCCESS, payload: {}}));
initReceived = true;
}
if (parsedMessage.type === SUBSCRIPTION_START) {
expect(initReceived).to.be.true;
client.unsubscribeAll();
done();
}
});
});
client.subscribe(
{
query: `subscription useInfo {
user(id: 3) {
id
name
}
}`,
},
(error, result) => {
// do nothing
}
);
});

it('should emit connect event for client side when socket is open', (done) => {
const client = new SubscriptionClient(`ws://localhost:${TEST_PORT}/`);

Expand Down Expand Up @@ -304,9 +338,6 @@ describe('Client', function () {
}).to.throw();
});




it('should allow both data and errors on SUBSCRIPTION_DATA', (done) => {
wsServer.on('connection', (connection: any) => {
connection.on('message', (message: any) => {
Expand Down

0 comments on commit 7dba040

Please sign in to comment.