-
Notifications
You must be signed in to change notification settings - Fork 107
/
RealtimeService.ts
403 lines (344 loc) · 13.3 KB
/
RealtimeService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import BaseService from '@/services/utils/BaseService';
import ClientResponseError from '@/ClientResponseError';
interface promiseCallbacks {
resolve: Function
reject: Function
}
export type UnsubscribeFunc = () => Promise<void>;
export default class RealtimeService extends BaseService {
private clientId: string = "";
private eventSource: EventSource | null = null;
private subscriptions: { [key: string]: Array<EventListener> } = {};
private lastSentTopics: Array<string> = [];
private connectTimeoutId: any;
private maxConnectTimeout: number = 15000;
private reconnectTimeoutId: any;
private reconnectAttempts: number = 0;
private maxReconnectAttempts: number = Infinity;
private predefinedReconnectIntervals: Array<number> = [
200, 300, 500, 1000, 1200, 1500, 2000,
];
private pendingConnects: Array<promiseCallbacks> = [];
/**
* Returns whether the realtime connection has been established.
*/
get isConnected(): boolean {
return !!this.eventSource && !!this.clientId && !this.pendingConnects.length;
}
/**
* Register the subscription listener.
*
* You can subscribe multiple times to the same topic.
*
* If the SSE connection is not started yet,
* this method will also initialize it.
*/
async subscribe(topic: string, callback: (data: any) => void): Promise<UnsubscribeFunc> {
if (!topic) {
throw new Error('topic must be set.')
}
const listener = function (e: Event) {
const msgEvent = (e as MessageEvent);
let data;
try {
data = JSON.parse(msgEvent?.data);
} catch {}
callback(data || {});
};
// store the listener
if (!this.subscriptions[topic]) {
this.subscriptions[topic] = [];
}
this.subscriptions[topic].push(listener);
if (!this.isConnected) {
// initialize sse connection
await this.connect();
} else if (this.subscriptions[topic].length === 1) {
// send the updated subscriptions (if it is the first for the topic)
await this.submitSubscriptions();
} else {
// only register the listener
this.eventSource?.addEventListener(topic, listener);
}
return async (): Promise<void> => {
return this.unsubscribeByTopicAndListener(topic, listener);
};
}
/**
* Unsubscribe from all subscription listeners with the specified topic.
*
* If `topic` is not provided, then this method will unsubscribe
* from all active subscriptions.
*
* This method is no-op if there are no active subscriptions.
*
* The related sse connection will be autoclosed if after the
* unsubscribe operation there are no active subscriptions left.
*/
async unsubscribe(topic?: string): Promise<void> {
if (!this.hasSubscriptionListeners(topic)) {
return; // already unsubscribed
}
if (!topic) {
// remove all subscriptions
this.subscriptions = {};
} else {
// remove all topic listeners
for (let listener of this.subscriptions[topic]) {
this.eventSource?.removeEventListener(topic, listener);
}
delete this.subscriptions[topic];
}
if (!this.hasSubscriptionListeners()) {
// no other active subscriptions -> close the sse connection
this.disconnect();
} else if (!this.hasSubscriptionListeners(topic)) {
// submit subscriptions change if there are no other active subscriptions related to the topic
await this.submitSubscriptions();
}
}
/**
* Unsubscribe from all subscription listeners starting with the specified topic prefix.
*
* This method is no-op if there are no active subscriptions with the specified topic prefix.
*
* The related sse connection will be autoclosed if after the
* unsubscribe operation there are no active subscriptions left.
*/
async unsubscribeByPrefix(topicPrefix: string): Promise<void> {
let hasAtleastOneTopic = false;
for (let topic in this.subscriptions) {
if (!topic.startsWith(topicPrefix)) {
continue;
}
hasAtleastOneTopic = true;
for (let listener of this.subscriptions[topic]) {
this.eventSource?.removeEventListener(topic, listener);
}
delete this.subscriptions[topic];
}
if (!hasAtleastOneTopic) {
return; // nothing to unsubscribe from
}
if (this.hasSubscriptionListeners()) {
// submit the deleted subscriptions
await this.submitSubscriptions();
} else {
// no other active subscriptions -> close the sse connection
this.disconnect();
}
}
/**
* Unsubscribe from all subscriptions matching the specified topic and listener function.
*
* This method is no-op if there are no active subscription with
* the specified topic and listener.
*
* The related sse connection will be autoclosed if after the
* unsubscribe operation there are no active subscriptions left.
*/
async unsubscribeByTopicAndListener(topic: string, listener: EventListener): Promise<void> {
if (!Array.isArray(this.subscriptions[topic]) || !this.subscriptions[topic].length) {
return; // already unsubscribed
}
let exist = false;
for (let i = this.subscriptions[topic].length - 1; i >= 0; i--) {
if (this.subscriptions[topic][i] !== listener) {
continue;
}
exist = true; // has at least one matching listener
delete this.subscriptions[topic][i]; // removes the function reference
this.subscriptions[topic].splice(i, 1); // reindex the array
this.eventSource?.removeEventListener(topic, listener);
}
if (!exist) {
return;
}
// remove the topic from the subscriptions list if there are no other listeners
if (!this.subscriptions[topic].length) {
delete this.subscriptions[topic];
}
if (!this.hasSubscriptionListeners()) {
// no other active subscriptions -> close the sse connection
this.disconnect();
} else if (!this.hasSubscriptionListeners(topic)) {
// submit subscriptions change if there are no other active subscriptions related to the topic
await this.submitSubscriptions();
}
}
private hasSubscriptionListeners(topicToCheck?: string): boolean {
this.subscriptions = this.subscriptions || {};
// check the specified topic
if (topicToCheck) {
return !!this.subscriptions[topicToCheck]?.length;
}
// check for at least one non-empty topic
for (let topic in this.subscriptions) {
if (!!this.subscriptions[topic]?.length) {
return true
}
}
return false;
}
private async submitSubscriptions(): Promise<void> {
if (!this.clientId) {
return; // no client/subscriber
}
// optimistic update
this.addAllSubscriptionListeners();
this.lastSentTopics = this.getNonEmptySubscriptionTopics();
return this.client.send('/api/realtime', {
'method': 'POST',
'body': {
'clientId': this.clientId,
'subscriptions': this.lastSentTopics,
},
'params': {
'$cancelKey': "realtime_" + this.clientId,
},
}).catch((err) => {
if (err?.isAbort) {
return; // silently ignore aborted pending requests
}
throw err;
});
}
private getNonEmptySubscriptionTopics(): Array<string> {
const result : Array<string> = [];
for (let topic in this.subscriptions) {
if (this.subscriptions[topic].length) {
result.push(topic);
}
}
return result;
}
private addAllSubscriptionListeners(): void {
if (!this.eventSource) {
return;
}
this.removeAllSubscriptionListeners();
for (let topic in this.subscriptions) {
for (let listener of this.subscriptions[topic]) {
this.eventSource.addEventListener(topic, listener);
}
}
}
private removeAllSubscriptionListeners(): void {
if (!this.eventSource) {
return;
}
for (let topic in this.subscriptions) {
for (let listener of this.subscriptions[topic]) {
this.eventSource.removeEventListener(topic, listener);
}
}
}
private async connect(): Promise<void> {
if (this.reconnectAttempts > 0) {
// immediately resolve the promise to avoid indefinitely
// blocking the client during reconnection
return;
}
return new Promise((resolve, reject) => {
this.pendingConnects.push({ resolve, reject });
if (this.pendingConnects.length > 1) {
// all promises will be resolved once the connection is established
return;
}
this.initConnect();
})
}
private initConnect() {
this.disconnect(true);
// wait up to 15s for connect
clearTimeout(this.connectTimeoutId);
this.connectTimeoutId = setTimeout(() => {
this.connectErrorHandler(new Error("EventSource connect took too long."));
}, this.maxConnectTimeout);
this.eventSource = new EventSource(this.client.buildUrl('/api/realtime'))
this.eventSource.onerror = (_) => {
this.connectErrorHandler(new Error("Failed to establish realtime connection."));
};
this.eventSource.addEventListener('PB_CONNECT', (e) => {
const msgEvent = (e as MessageEvent);
this.clientId = msgEvent?.lastEventId;
this.submitSubscriptions()
.then(async () => {
let retries = 3;
while (this.hasUnsentSubscriptions() && retries > 0) {
retries--;
// resubscribe to ensure that the latest topics are submitted
//
// This is needed because missed topics could happen on reconnect
// if after the pending sent `submitSubscriptions()` call another `subscribe()`
// was made before the submit was able to complete.
await this.submitSubscriptions();
}
}).then(() => {
for (let p of this.pendingConnects) {
p.resolve();
}
// reset connect meta
this.pendingConnects = [];
this.reconnectAttempts = 0;
clearTimeout(this.reconnectTimeoutId);
clearTimeout(this.connectTimeoutId);
}).catch((err) => {
this.clientId = "";
this.connectErrorHandler(err);
});
});
}
private hasUnsentSubscriptions(): boolean {
const latestTopics = this.getNonEmptySubscriptionTopics();
if (latestTopics.length != this.lastSentTopics.length) {
return true;
}
for (const t of latestTopics) {
if (!this.lastSentTopics.includes(t)) {
return true;
}
}
return false;
}
private connectErrorHandler(err: any) {
clearTimeout(this.connectTimeoutId);
clearTimeout(this.reconnectTimeoutId);
if (
// wasn't previously connected -> direct reject
(!this.clientId && !this.reconnectAttempts) ||
// was previously connected but the max reconnection limit has been reached
this.reconnectAttempts > this.maxReconnectAttempts
) {
for (let p of this.pendingConnects) {
p.reject(new ClientResponseError(err));
}
this.disconnect();
return;
}
// otherwise -> reconnect in the background
this.disconnect(true);
const timeout = this.predefinedReconnectIntervals[this.reconnectAttempts] || this.predefinedReconnectIntervals[this.predefinedReconnectIntervals.length - 1];
this.reconnectAttempts++;
this.reconnectTimeoutId = setTimeout(() => {
this.initConnect();
}, timeout);
}
private disconnect(fromReconnect = false): void {
clearTimeout(this.connectTimeoutId);
clearTimeout(this.reconnectTimeoutId);
this.removeAllSubscriptionListeners();
this.eventSource?.close();
this.eventSource = null;
this.clientId = "";
if (!fromReconnect) {
this.reconnectAttempts = 0;
// reject any remaining connect promises
const err = new ClientResponseError(new Error("Realtime disconnected."));
for (let p of this.pendingConnects) {
p.reject(err);
}
this.pendingConnects = [];
}
}
}