Skip to content

Commit 5bf87d8

Browse files
authored
Improve WebSocketServer Error Handling (#6230)
* Improve WebSocketServer Error Handling Closes: #6173 Prevents an unhandled server rejection. Includes an example for LiveQuery test and closing the proper connections. Improve live query monitoring * fix tests
1 parent dff6825 commit 5bf87d8

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed

spec/ParseLiveQuery.spec.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
describe('ParseLiveQuery', function() {
4+
it('can subscribe to query', async done => {
5+
await reconfigureServer({
6+
liveQuery: {
7+
classNames: ['TestObject'],
8+
},
9+
startLiveQueryServer: true,
10+
verbose: false,
11+
silent: true,
12+
});
13+
const object = new TestObject();
14+
await object.save();
15+
16+
const query = new Parse.Query(TestObject);
17+
query.equalTo('objectId', object.id);
18+
const subscription = await query.subscribe();
19+
subscription.on('update', async object => {
20+
expect(object.get('foo')).toBe('bar');
21+
done();
22+
});
23+
object.set({ foo: 'bar' });
24+
await object.save();
25+
});
26+
27+
afterEach(async function(done) {
28+
const client = await Parse.CoreManager.getLiveQueryController().getDefaultLiveQueryClient();
29+
client.close();
30+
// Wait for live query client to disconnect
31+
setTimeout(() => {
32+
done();
33+
}, 1000);
34+
});
35+
});

spec/ParseWebSocketServer.spec.js

+39
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,45 @@ describe('ParseWebSocketServer', function() {
3939
}, 10);
4040
});
4141

42+
it('can handle error event', async () => {
43+
jasmine.restoreLibrary('ws', 'Server');
44+
const WebSocketServer = require('ws').Server;
45+
let wssError;
46+
class WSSAdapter {
47+
constructor(options) {
48+
this.options = options;
49+
}
50+
onListen() {}
51+
onConnection() {}
52+
onError() {}
53+
start() {
54+
const wss = new WebSocketServer({ server: this.options.server });
55+
wss.on('listening', this.onListen);
56+
wss.on('connection', this.onConnection);
57+
wss.on('error', error => {
58+
wssError = error;
59+
this.onError(error);
60+
});
61+
this.wss = wss;
62+
}
63+
}
64+
65+
const server = await reconfigureServer({
66+
liveQuery: {
67+
classNames: ['TestObject'],
68+
},
69+
liveQueryServerOptions: {
70+
wssAdapter: WSSAdapter,
71+
},
72+
startLiveQueryServer: true,
73+
verbose: false,
74+
silent: true,
75+
});
76+
const wssAdapter = server.liveQueryServer.parseWebSocketServer.server;
77+
wssAdapter.wss.emit('error', 'Invalid Packet');
78+
expect(wssError).toBe('Invalid Packet');
79+
});
80+
4281
afterEach(function() {
4382
jasmine.restoreLibrary('ws', 'Server');
4483
});

src/Adapters/WebSocketServer/WSAdapter.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ export class WSAdapter extends WSSAdapter {
1313

1414
onListen() {}
1515
onConnection(ws) {}
16+
onError(error) {}
1617
start() {
1718
const wss = new WebSocketServer({ server: this.options.server });
1819
wss.on('listening', this.onListen);
1920
wss.on('connection', this.onConnection);
21+
wss.on('error', this.onError);
2022
}
2123
close() {}
2224
}

src/Adapters/WebSocketServer/WSSAdapter.js

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Adapter classes must implement the following functions:
55
// * onListen()
66
// * onConnection(ws)
7+
// * onError(error)
78
// * start()
89
// * close()
910
//
@@ -22,6 +23,7 @@ export class WSSAdapter {
2223
constructor(options) {
2324
this.onListen = () => {};
2425
this.onConnection = () => {};
26+
this.onError = () => {};
2527
}
2628

2729
// /**
@@ -36,6 +38,13 @@ export class WSSAdapter {
3638
// */
3739
// onConnection(ws) {}
3840

41+
// /**
42+
// * Emitted when error event is called.
43+
// *
44+
// * @param {Error} error - WebSocketServer error
45+
// */
46+
// onError(error) {}
47+
3948
/**
4049
* Initialize Connection.
4150
*

src/LiveQuery/ParseLiveQueryServer.js

+2
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ class ParseLiveQueryServer {
392392
event: 'ws_disconnect',
393393
clients: this.clients.size,
394394
subscriptions: this.subscriptions.size,
395+
useMasterKey: client.hasMasterKey,
396+
installationId: client.installationId,
395397
});
396398
});
397399

src/LiveQuery/ParseWebSocketServer.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export class ParseWebSocketServer {
2323
}
2424
}, config.websocketTimeout || 10 * 1000);
2525
};
26+
wss.onError = error => {
27+
logger.error(error);
28+
};
2629
wss.start();
2730
this.server = wss;
2831
}

0 commit comments

Comments
 (0)