Skip to content

Commit

Permalink
feat(shared-worker): move heartbeat call into SharedWorker
Browse files Browse the repository at this point in the history
Pass heartbeat request through `SharedWorker`` (if used) to optimize the number of requests for
clients opened in few tabs and subscribed on same channels / groups list.

refactor(heartbeat): remove redundant `heartbeat` from unsubscribe

Don't send `heartbeat` request on unsubscribe.
  • Loading branch information
parfeon committed Jan 30, 2025
1 parent f5f9670 commit 226a706
Show file tree
Hide file tree
Showing 8 changed files with 600 additions and 47 deletions.
19 changes: 14 additions & 5 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -3078,7 +3078,7 @@
}
makeSendable(req) {
// Use default request flow for non-subscribe / presence leave requests.
if (!req.path.startsWith('/v2/subscribe') && !req.path.endsWith('/leave'))
if (!req.path.startsWith('/v2/subscribe') && !req.path.endsWith('/heartbeat') && !req.path.endsWith('/leave'))
return this.configuration.transport.makeSendable(req);
let controller;
const sendRequestEvent = {
Expand Down Expand Up @@ -3188,6 +3188,7 @@
clientIdentifier: this.configuration.clientIdentifier,
subscriptionKey: this.configuration.subscriptionKey,
userId: this.configuration.userId,
heartbeatInterval: this.configuration.heartbeatInterval,
logVerbosity: this.configuration.logVerbosity,
workerLogVerbosity: this.configuration.workerLogVerbosity,
}, true);
Expand All @@ -3212,7 +3213,7 @@
else if (data.type === 'shared-worker-console-dir') {
if (data.message)
console.log(`[SharedWorker] ${data.message}`);
console.dir(data.data);
console.dir(data.data, { depth: 10 });
}
else if (data.type === 'shared-worker-ping') {
const { logVerbosity, subscriptionKey, clientIdentifier } = this.configuration;
Expand Down Expand Up @@ -4708,9 +4709,16 @@
this.stopHeartbeatTimer();
this.reconnectionManager.stopPolling();
}
reconnect() {
/**
* Restart subscription loop with current state.
*
* @param forUnsubscribe - Whether restarting subscription loop as part of channels list change on
* unsubscribe or not.
*/
reconnect(forUnsubscribe = false) {
this.startSubscribeLoop();
this.startHeartbeatTimer();
if (!forUnsubscribe)
this.startHeartbeatTimer();
}
/**
* Update channels and groups used in subscription loop.
Expand Down Expand Up @@ -4809,7 +4817,7 @@
this.region = null;
this.reconnectionManager.stopPolling();
}
this.reconnect();
this.reconnect(true);
}
unsubscribeAll(isOffline) {
this.unsubscribe({
Expand Down Expand Up @@ -14546,6 +14554,7 @@
userId: clientConfiguration.getUserId(),
workerUrl: configurationCopy.subscriptionWorkerUrl,
sdkVersion: clientConfiguration.getVersion(),
heartbeatInterval: clientConfiguration.getHeartbeatInterval(),
logVerbosity: clientConfiguration.logVerbosity,
workerLogVerbosity: platformConfiguration.subscriptionWorkerLogVerbosity,
transport,
Expand Down
4 changes: 2 additions & 2 deletions dist/web/pubnub.min.js

Large diffs are not rendered by default.

258 changes: 241 additions & 17 deletions dist/web/pubnub.worker.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/web/pubnub.worker.min.js

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions src/core/components/subscription-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,15 @@ export class SubscriptionManager {
this.reconnectionManager.stopPolling();
}

public reconnect() {
/**
* Restart subscription loop with current state.
*
* @param forUnsubscribe - Whether restarting subscription loop as part of channels list change on
* unsubscribe or not.
*/
public reconnect(forUnsubscribe: boolean = false) {
this.startSubscribeLoop();
this.startHeartbeatTimer();
if (!forUnsubscribe) this.startHeartbeatTimer();
}

/**
Expand Down Expand Up @@ -315,7 +321,7 @@ export class SubscriptionManager {
this.reconnectionManager.stopPolling();
}

this.reconnect();
this.reconnect(true);
}

public unsubscribeAll(isOffline?: boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ type PubNubMiddlewareConfiguration = {
*/
workerLogVerbosity: boolean;

/**
* How often the client will announce itself to server. The value is in seconds.
*
* @default `not set`
*/
heartbeatInterval?: number;

/**
* Platform-specific transport for requests processing.
*/
Expand Down Expand Up @@ -100,7 +107,7 @@ export class SubscriptionWorkerMiddleware implements Transport {

makeSendable(req: TransportRequest): [Promise<TransportResponse>, CancellationController | undefined] {
// Use default request flow for non-subscribe / presence leave requests.
if (!req.path.startsWith('/v2/subscribe') && !req.path.endsWith('/leave'))
if (!req.path.startsWith('/v2/subscribe') && !req.path.endsWith('/heartbeat') && !req.path.endsWith('/leave'))
return this.configuration.transport.makeSendable(req);

let controller: CancellationController | undefined;
Expand Down Expand Up @@ -225,6 +232,7 @@ export class SubscriptionWorkerMiddleware implements Transport {
clientIdentifier: this.configuration.clientIdentifier,
subscriptionKey: this.configuration.subscriptionKey,
userId: this.configuration.userId,
heartbeatInterval: this.configuration.heartbeatInterval,
logVerbosity: this.configuration.logVerbosity,
workerLogVerbosity: this.configuration.workerLogVerbosity,
},
Expand Down Expand Up @@ -254,7 +262,7 @@ export class SubscriptionWorkerMiddleware implements Transport {
console.log(`[SharedWorker] ${data.message}`);
} else if (data.type === 'shared-worker-console-dir') {
if (data.message) console.log(`[SharedWorker] ${data.message}`);
console.dir(data.data);
console.dir(data.data, { depth: 10 });
} else if (data.type === 'shared-worker-ping') {
const { logVerbosity, subscriptionKey, clientIdentifier } = this.configuration;

Expand Down
Loading

0 comments on commit 226a706

Please sign in to comment.