Skip to content

Commit 36e0ec3

Browse files
authored
Fix Shared Worker state update on presence change and listen for pagehide (#445)
feat(shared-worker): early request terminate on pagehide On `pagehide` without `bfcache` client on page will send `terminate` to Shared Worker for early long-poll request termination and `leave` request sending (if configured). fix(presence): fix channels and groups update in Shared worker Fix an issue with the client's state update in Shared Worker caused by `-pnpres` suffixed entries being removed from heartbeat / leave request channels and groups.
1 parent e80c970 commit 36e0ec3

File tree

17 files changed

+337
-125
lines changed

17 files changed

+337
-125
lines changed

.pubnub.yml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
changelog:
3+
- date: 2025-03-19
4+
version: v9.2.0
5+
changes:
6+
- type: feature
7+
text: "On `pagehide` without `bfcache` client on page will send `terminate` to Shared Worker for early long-poll request termination and `leave` request sending (if configured)."
8+
- type: bug
9+
text: "Fix an issue with the client's state update in Shared Worker caused by `-pnpres` suffixed entries being removed from heartbeat / leave request channels and groups."
310
- date: 2025-03-13
411
version: v9.1.0
512
changes:
@@ -1178,7 +1185,7 @@ supported-platforms:
11781185
- 'Ubuntu 14.04 and up'
11791186
- 'Windows 7 and up'
11801187
version: 'Pubnub Javascript for Node'
1181-
version: '9.1.0'
1188+
version: '9.2.0'
11821189
sdks:
11831190
- full-name: PubNub Javascript SDK
11841191
short-name: Javascript
@@ -1194,7 +1201,7 @@ sdks:
11941201
- distribution-type: source
11951202
distribution-repository: GitHub release
11961203
package-name: pubnub.js
1197-
location: https://github.com/pubnub/javascript/archive/refs/tags/v9.1.0.zip
1204+
location: https://github.com/pubnub/javascript/archive/refs/tags/v9.2.0.zip
11981205
requires:
11991206
- name: 'agentkeepalive'
12001207
min-version: '3.5.2'
@@ -1865,7 +1872,7 @@ sdks:
18651872
- distribution-type: library
18661873
distribution-repository: GitHub release
18671874
package-name: pubnub.js
1868-
location: https://github.com/pubnub/javascript/releases/download/v9.1.0/pubnub.9.1.0.js
1875+
location: https://github.com/pubnub/javascript/releases/download/v9.2.0/pubnub.9.2.0.js
18691876
requires:
18701877
- name: 'agentkeepalive'
18711878
min-version: '3.5.2'

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## v9.2.0
2+
March 19 2025
3+
4+
#### Added
5+
- On `pagehide` without `bfcache` client on page will send `terminate` to Shared Worker for early long-poll request termination and `leave` request sending (if configured).
6+
7+
#### Fixed
8+
- Fix an issue with the client's state update in Shared Worker caused by `-pnpres` suffixed entries being removed from heartbeat / leave request channels and groups.
9+
110
## v9.1.0
211
March 13 2025
312

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
2727
npm install pubnub
2828
```
2929
* or download one of our builds from our CDN:
30-
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.1.0.js
31-
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.1.0.min.js
30+
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.2.0.js
31+
* https://cdn.pubnub.com/sdk/javascript/pubnub.9.2.0.min.js
3232
3333
2. Configure your keys:
3434

dist/web/pubnub.js

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,6 +3179,17 @@
31793179
this.callbacks = new Map();
31803180
this.setupSubscriptionWorker();
31813181
}
3182+
/**
3183+
* Terminate all ongoing long-poll requests.
3184+
*/
3185+
terminate() {
3186+
this.scheduleEventPost({
3187+
type: 'client-unregister',
3188+
clientIdentifier: this.configuration.clientIdentifier,
3189+
subscriptionKey: this.configuration.subscriptionKey,
3190+
logVerbosity: this.configuration.logVerbosity,
3191+
});
3192+
}
31823193
makeSendable(req) {
31833194
// Use default request flow for non-subscribe / presence leave requests.
31843195
if (!req.path.startsWith('/v2/subscribe') && !req.path.endsWith('/heartbeat') && !req.path.endsWith('/leave'))
@@ -3430,7 +3441,7 @@
34303441
.join('|');
34313442
if (typeof crypto !== 'undefined' && crypto.subtle) {
34323443
const hash = yield crypto.subtle.digest('SHA-256', new TextEncoder().encode(accessToken));
3433-
accessToken = String.fromCharCode(...new Uint8Array(hash));
3444+
accessToken = String.fromCharCode(...Array.from(new Uint8Array(hash)));
34343445
}
34353446
return [token, typeof btoa !== 'undefined' ? btoa(accessToken) : accessToken];
34363447
});
@@ -3891,6 +3902,10 @@
38913902
getUseRandomIVs() {
38923903
return base.useRandomIVs;
38933904
},
3905+
getKeepPresenceChannelsInPresenceRequests() {
3906+
// @ts-expect-error: Access field from web-based SDK configuration.
3907+
return base.sdkFamily === 'Web' && base['subscriptionWorkerUrl'];
3908+
},
38943909
setPresenceTimeout(value) {
38953910
this.heartbeatInterval = value / 2 - 1;
38963911
this.presenceTimeout = value;
@@ -3917,7 +3932,7 @@
39173932
return base.PubNubFile;
39183933
},
39193934
get version() {
3920-
return '9.1.0';
3935+
return '9.2.0';
39213936
},
39223937
getVersion() {
39233938
return this.version;
@@ -13593,10 +13608,13 @@
1359313608
{
1359413609
// Filtering out presence channels and groups.
1359513610
let { channels, channelGroups } = parameters;
13596-
if (channelGroups)
13597-
channelGroups = channelGroups.filter((channelGroup) => !channelGroup.endsWith('-pnpres'));
13598-
if (channels)
13599-
channels = channels.filter((channel) => !channel.endsWith('-pnpres'));
13611+
// Remove `-pnpres` channels / groups if they not acceptable in current PubNub client configuration.
13612+
if (!this._configuration.getKeepPresenceChannelsInPresenceRequests()) {
13613+
if (channelGroups)
13614+
channelGroups = channelGroups.filter((channelGroup) => !channelGroup.endsWith('-pnpres'));
13615+
if (channels)
13616+
channels = channels.filter((channel) => !channel.endsWith('-pnpres'));
13617+
}
1360013618
// Complete immediately request only for presence channels.
1360113619
if ((channelGroups !== null && channelGroups !== void 0 ? channelGroups : []).length === 0 && (channels !== null && channels !== void 0 ? channels : []).length === 0) {
1360213620
return callback({
@@ -13959,10 +13977,13 @@
1395913977
{
1396013978
// Filtering out presence channels and groups.
1396113979
let { channels, channelGroups } = parameters;
13962-
if (channelGroups)
13963-
channelGroups = channelGroups.filter((channelGroup) => !channelGroup.endsWith('-pnpres'));
13964-
if (channels)
13965-
channels = channels.filter((channel) => !channel.endsWith('-pnpres'));
13980+
// Remove `-pnpres` channels / groups if they not acceptable in current PubNub client configuration.
13981+
if (!this._configuration.getKeepPresenceChannelsInPresenceRequests()) {
13982+
if (channelGroups)
13983+
channelGroups = channelGroups.filter((channelGroup) => !channelGroup.endsWith('-pnpres'));
13984+
if (channels)
13985+
channels = channels.filter((channel) => !channel.endsWith('-pnpres'));
13986+
}
1396613987
// Complete immediately request only for presence channels.
1396713988
if ((channelGroups !== null && channelGroups !== void 0 ? channelGroups : []).length === 0 && (channels !== null && channels !== void 0 ? channels : []).length === 0) {
1396813989
const responseStatus = {
@@ -14789,7 +14810,7 @@
1478914810
{
1479014811
if (configurationCopy.subscriptionWorkerUrl) {
1479114812
// Inject subscription worker into transport provider stack.
14792-
transport = new SubscriptionWorkerMiddleware({
14813+
const middleware = new SubscriptionWorkerMiddleware({
1479314814
clientIdentifier: clientConfiguration._instanceId,
1479414815
subscriptionKey: clientConfiguration.subscribeKey,
1479514816
userId: clientConfiguration.getUserId(),
@@ -14803,6 +14824,11 @@
1480314824
tokenManager,
1480414825
transport,
1480514826
});
14827+
transport = middleware;
14828+
window.onpagehide = (event) => {
14829+
if (!event.persisted)
14830+
middleware.terminate();
14831+
};
1480614832
}
1480714833
}
1480814834
const transportMiddleware = new PubNubMiddleware({

dist/web/pubnub.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)