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

Commit

Permalink
Emit event when an websocket error occurs.
Browse files Browse the repository at this point in the history
  • Loading branch information
damour authored and damour committed Mar 22, 2018
1 parent e9456ed commit 1a3d0e6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Changelog

### vNEXT
<<<<<<< 91a6bb7c7723c4c2178fc25260208a54a33071c1
- added `error` event to handle connection errors and debug network troubles [PR #341](https://github.com/apollographql/subscriptions-transport-ws/pull/341).

### v0.9.7
- change default timeout from 10s to 30s [PR #368](https://github.com/apollographql/subscriptions-transport-ws/pull/368)
- pass `request` (`upgradeReq`) to `ConnectionContext` [PR #369](https://github.com/apollographql/subscriptions-transport-ws/pull/369)
- pass `ConnectionContext` to `onDisconnect()` as second argument [PR #369](https://github.com/apollographql/subscriptions-transport-ws/pull/369)

### 0.9.6
=======
- Added `error` event to handle errors and debug network troubles [PR #341](https://github.com/apollographql/subscriptions-transport-ws/pull/341).
>>>>>>> Rename event.
- fix shallow cloning on contexts which are classes
- upgrade to support graphql 0.13.X
- bump iterall version [PR #362](https://github.com/apollographql/subscriptions-transport-ws/pull/362)
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ ReactDOM.render(
#### `unsubscribeAll() => void` - unsubscribes from all active subscriptions.

#### `on(eventName, callback, thisContext) => Function`
- `eventName: string`: the name of the event, available events are: `connecting`, `connected`, `reconnecting`, `reconnected` and `disconnected`
- `eventName: string`: the name of the event, available events are: `connecting`, `connected`, `reconnecting`, `reconnected`, `disconnected` and `error`
- `callback: Function`: function to be called when websocket connects and initialized.
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.
Expand Down Expand Up @@ -285,6 +285,11 @@ ReactDOM.render(
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

#### `onError(callback, thisContext) => Function` - shorthand for `.on('error', ...)`
- `callback: Function`: function to be called when an error occurs.
- `thisContext: any`: `this` context to use when calling the callback function.
- => Returns an `off` method to cancel the event subscription.

### `close() => void` - closes the WebSocket connection manually, and ignores `reconnect` logic if it was set to `true`.

### `use(middlewares: MiddlewareInterface[]) => SubscriptionClient` - adds middleware to modify `OperationOptions` per each request
Expand Down
7 changes: 6 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ export class SubscriptionClient {
return this.on('reconnecting', callback, context);
}

public onError(callback: ListenerFn, context?: any): Function {
return this.on('error', callback, context);
}

public unsubscribeAll() {
Object.keys(this.operations).forEach( subId => {
this.unsubscribe(subId);
Expand Down Expand Up @@ -514,9 +518,10 @@ export class SubscriptionClient {
}
};

this.client.onerror = () => {
this.client.onerror = (err: Error) => {
// Capture and ignore errors to prevent unhandled exceptions, wait for
// onclose to fire before attempting a reconnect.
this.eventEmitter.emit('error', err);
};

this.client.onmessage = ({ data }: {data: any}) => {
Expand Down
20 changes: 20 additions & 0 deletions src/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,26 @@ describe('Client', function () {
}, 1000);
});

it('should emit event when an websocket error occurs', function (done) {
const client = new SubscriptionClient(`ws://localhost:${ERROR_TEST_PORT}/`);

client.request({
query: `subscription useInfo{
invalid
}`,
variables: {},
}).subscribe({
next: () => {
assert(false);
},
});

client.onError((err: Error) => {
expect(err.message).to.be.equal(`connect ECONNREFUSED 127.0.0.1:${ERROR_TEST_PORT}`);
done();
});
});

it('should stop trying to reconnect to the server', function (done) {
wsServer.on('connection', (connection: WebSocket) => {
connection.close();
Expand Down

0 comments on commit 1a3d0e6

Please sign in to comment.