Skip to content

Commit 39a639f

Browse files
author
Shiranuit
authored
Merge pull request #639 from kuzzleio/638-disconnection-origin
Add an origin parameter to the kuzzle disconnected event
2 parents a967b88 + 8eb5c4b commit 39a639f

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

doc/7/essentials/events/index.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ Triggered when Kuzzle discards a request, typically if no connection is establis
3232

3333
Triggered when the current session has been unexpectedly disconnected.
3434

35+
<SinceBadge version="auto-version"/>
36+
**Callback arguments:**
37+
38+
`@param {object} context`
39+
40+
| Property | Type | Description |
41+
| -------- | ----------------- | ------------------------------------------ |
42+
| `origin` | <pre>string</pre> | Indicate what is causing the disconnection |
43+
44+
**Origins**
45+
46+
| Name | Description |
47+
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
48+
| `websocket/auth-renewal` | The websocket protocol si reconnecting to renew the token. See [Websocket Cookie Authentication](sdk/js/7/protocols/websocket/introduction#cookie-authentication). |
49+
| `user/connection-closed` | The disconnection is done by the user. |
50+
| `network/error` | An network error occured and caused a disconnection. |
3551
## loginAttempt
3652

3753
Triggered when a login attempt completes, either with a success or a failure result.
@@ -75,8 +91,8 @@ Triggered whenever a request is added to the offline queue.
7591

7692
`@param {object} data`
7793

78-
| Property | Type | Description |
79-
| --------- | ----------------- | ------------------------------------------------------------------ |
94+
| Property | Type | Description |
95+
| --------- | ----------------- | ------------------------------------------------------------------- |
8096
| `request` | <pre>object</pre> | [Request](/core/2/guides/main-concepts/querying) added to the queue |
8197

8298
## queryError

src/Kuzzle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,8 @@ export class Kuzzle extends KuzzleEventEmitter {
521521
this.emit('networkError', error);
522522
});
523523

524-
this.protocol.addListener('disconnect', () => {
525-
this.emit('disconnected');
524+
this.protocol.addListener('disconnect', context => {
525+
this.emit('disconnected', context);
526526
});
527527

528528
this.protocol.addListener('reconnect', () => {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const WEBSOCKET_AUTH_RENEWAL = 'websocket/auth-renewal';
2+
const USER_CONNECTION_CLOSED = 'user/connection-closed';
3+
const NETWORK_ERROR = 'network/error';
4+
5+
export {
6+
WEBSOCKET_AUTH_RENEWAL,
7+
USER_CONNECTION_CLOSED,
8+
NETWORK_ERROR,
9+
};

src/protocols/WebSocket.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BaseProtocolRealtime } from './abstract/Realtime';
55
import { JSONObject } from '../types';
66
import { RequestPayload } from '../types/RequestPayload';
77
import HttpProtocol from './Http';
8+
import * as DisconnectionOrigin from './DisconnectionOrigin';
89

910
/**
1011
* WebSocket protocol used to connect to a Kuzzle server.
@@ -145,7 +146,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
145146
}
146147

147148
if (status === 1000) {
148-
this.clientDisconnected();
149+
this.clientDisconnected(DisconnectionOrigin.USER_CONNECTION_CLOSED);
149150
}
150151
// do not forward a connection close error if no
151152
// connection has been previously established
@@ -266,7 +267,7 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
266267
this.client.close();
267268
}
268269
this.client = null;
269-
this.clientDisconnected(); // Simulate a disconnection, this will enable offline queue and trigger realtime subscriptions backup
270+
this.clientDisconnected(DisconnectionOrigin.WEBSOCKET_AUTH_RENEWAL); // Simulate a disconnection, this will enable offline queue and trigger realtime subscriptions backup
270271

271272
this._httpProtocol._sendHttpRequest(formattedRequest)
272273
.then(response => {
@@ -284,10 +285,10 @@ export default class WebSocketProtocol extends BaseProtocolRealtime {
284285
/**
285286
* @override
286287
*/
287-
clientDisconnected() {
288+
clientDisconnected(origin: string) {
288289
clearInterval(this.pingIntervalId);
289290
clearTimeout(this.pongTimeoutId);
290-
super.clientDisconnected();
291+
super.clientDisconnected(origin);
291292
}
292293

293294
/**

src/protocols/abstract/Realtime.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
import { KuzzleAbstractProtocol } from './Base';
4+
import * as DisconnectionOrigin from '../DisconnectionOrigin';
45

56
export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
67
protected _autoReconnect: boolean;
@@ -51,10 +52,12 @@ export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
5152

5253
/**
5354
* Called when the client's connection is closed
55+
*
56+
* @param {string} origin String that describe what is causing the disconnection
5457
*/
55-
clientDisconnected () {
58+
clientDisconnected (origin: string) {
5659
this.clear();
57-
this.emit('disconnect');
60+
this.emit('disconnect', { origin });
5861
}
5962

6063
/**
@@ -93,7 +96,7 @@ export abstract class BaseProtocolRealtime extends KuzzleAbstractProtocol {
9396
}, this.reconnectionDelay);
9497
}
9598
else {
96-
this.emit('disconnect');
99+
this.emit('disconnect', { origin: DisconnectionOrigin.NETWORK_ERROR });
97100
}
98101
}
99102

test/mocks/protocol.mock.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const
22
sinon = require('sinon'),
3-
{ KuzzleEventEmitter } = require('../../src/core/KuzzleEventEmitter');
3+
{ KuzzleEventEmitter } = require('../../src/core/KuzzleEventEmitter'),
4+
DisconnectionOrigin = require('../../src/protocols/DisconnectionOrigin');
45

56
class ProtocolMock extends KuzzleEventEmitter {
67

@@ -54,7 +55,7 @@ class ProtocolMock extends KuzzleEventEmitter {
5455

5556
disconnect () {
5657
this.state = 'offline';
57-
this.emit('disconnect');
58+
this.emit('disconnect', {origin: DisconnectionOrigin.USER_CONNECTION_CLOSED });
5859
}
5960

6061
send (request) {

test/protocol/WebSocket.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const NodeWS = require('ws');
66
const { default: WS } = require('../../src/protocols/WebSocket');
77
const windowMock = require('../mocks/window.mock');
88
const { default: HttpProtocol } = require('../../src/protocols/Http');
9+
const DisconnectionOrigin = require('../../src/protocols/DisconnectionOrigin');
910

1011
describe('WebSocket networking module', () => {
1112
let
@@ -160,6 +161,7 @@ describe('WebSocket networking module', () => {
160161
websocket.connect = sinon.stub().resolves();
161162
clientStub.onopen();
162163
clientStub.onclose(1000);
164+
should(cb).be.calledOnce().and.be.calledWith({origin: DisconnectionOrigin.USER_CONNECTION_CLOSED });
163165
websocket.close();
164166
should(clearTimeout)
165167
.be.calledOnce();
@@ -188,6 +190,7 @@ describe('WebSocket networking module', () => {
188190

189191
it('should not try to reconnect on a connection error with autoReconnect = false', () => {
190192
const cb = sinon.stub();
193+
const disconnectCB = sinon.stub();
191194

192195
websocket = new WS('address', {
193196
port: 1234,
@@ -196,6 +199,7 @@ describe('WebSocket networking module', () => {
196199
});
197200

198201
websocket.retrying = false;
202+
websocket.addListener('disconnect', disconnectCB);
199203
websocket.addListener('networkError', cb);
200204
should(websocket.listeners('networkError').length).be.eql(1);
201205

@@ -205,6 +209,7 @@ describe('WebSocket networking module', () => {
205209
clock.tick(10);
206210

207211
should(cb).be.calledOnce();
212+
should(disconnectCB).be.calledOnce().and.be.calledWith({ origin: DisconnectionOrigin.NETWORK_ERROR });
208213
should(websocket.retrying).be.false();
209214
should(websocket.connect).not.be.called();
210215

@@ -276,7 +281,7 @@ describe('WebSocket networking module', () => {
276281
clientStub.onclose(1000);
277282

278283
clock.tick(10);
279-
should(cb).be.calledOnce();
284+
should(cb).be.calledOnce().and.be.calledWith({ origin: DisconnectionOrigin.USER_CONNECTION_CLOSED });
280285
should(websocket.listeners('disconnect').length).be.eql(1);
281286
websocket.clear.should.be.calledOnce();
282287
});
@@ -724,7 +729,7 @@ describe('WebSocket networking module', () => {
724729
});
725730

726731
await should(clientStub.close).be.calledOnce();
727-
await should(websocket.clientDisconnected).be.calledOnce();
732+
await should(websocket.clientDisconnected).be.calledOnce().and.calledWith(DisconnectionOrigin.WEBSOCKET_AUTH_RENEWAL);
728733
await should(clientStub.send).not.be.called();
729734

730735
await should(onRenewalStart).be.calledOnce();

0 commit comments

Comments
 (0)