Skip to content

Commit

Permalink
WS is eventemitter now. So you can catch any events to debug ws conne…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
mrspartak committed May 26, 2020
1 parent 7c361f6 commit ef7f8f3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/theme/components/Footer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
target="_blank"
rel="noopener noreferrer"
>by Spartak</a>
<script async src="https://ackee.spartak.io/ackee.js" data-ackee-server="https://ackee.spartak.io" data-ackee-domain-id="84646694-75c6-4d3e-a6d0-0bd0db7089dc"></script>
</footer>
</template>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"orm",
"nodejs"
],
"homepage": "https://github.com/mrspartak/hasura-om",
"homepage": "https://mrspartak.github.io/hasura-om/",
"repository": {
"type": "git",
"url": "https://github.com/mrspartak/hasura-om.git"
Expand Down
41 changes: 22 additions & 19 deletions src/hasura/wsgql.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const EventEmitter = require('events');
const ws = require('ws');
const {SubscriptionClient} = require('subscriptions-transport-ws');

class WsGql {
class WsGql extends EventEmitter {
constructor(parameters) {
super();

const defaultParameters = {
wsUrl: null,
adminSecret: null,
Expand Down Expand Up @@ -38,24 +41,24 @@ class WsGql {
ws,
);

/* This.client.on('connected', (data) => {
console.log('client connected', data)
})
this.client.on('reconnected', (data) => {
console.log('client reconnected', data)
})
this.client.on('connecting', (data) => {
console.log('client connecting', data)
})
this.client.on('reconnecting', (data) => {
console.log('client reconnecting', data)
})
this.client.on('disconnected', (data) => {
console.log('client disconnected', data)
})
this.client.on('error', (data) => {
console.log('client error', data.message)
}) */
this.client.on('connecting', () => {
this.emit('connecting');
});
this.client.on('connected', () => {
this.emit('connected');
});
this.client.on('reconnected', () => {
this.emit('reconnected');
});
this.client.on('reconnecting', () => {
this.emit('reconnecting');
});
this.client.on('disconnected', () => {
this.emit('disconnected');
});
this.client.on('error', (error) => {
this.emit('error', error);
});
}

run({query, variables, callback, flat = (data) => data}) {
Expand Down
48 changes: 48 additions & 0 deletions tests/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,51 @@ test.serial('test sub', async (t) => {
);
});
});

test.serial('test events connect/disconnect', (t) => {
const orm = new Hasura({
graphqlUrl: process.env.GQL_ENDPOINT,
adminSecret: process.env.GQL_SECRET,
});
orm.createTable({
name: '_om_test',
});
orm.table('_om_test').setField({
name: 'id',
type: 'Int',
});
orm.table('_om_test').init();

return new Promise((resolve) => {
t.timeout(1000);

let passed = 0;
orm.$ws.on('connected', () => {
passed++;
});
orm.$ws.on('connecting', () => {
passed++;
});
orm.$ws.on('disconnected', () => {
passed++;
});

orm.subscribe(
{
_om_test: {
aggregate: {
count: {},
},
},
},
() => {},
);
setTimeout(() => {
orm.$ws.client.close();
}, 500);

setInterval(() => {
if (passed === 3) resolve();
}, 100);
});
});

0 comments on commit ef7f8f3

Please sign in to comment.