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

Emit event when an websocket error occurs. #341

Merged
merged 1 commit into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

### vNEXT
- 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)
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