Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions .pubnub.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
---
changelog:
- date: 2026-10-14
version: v13.1.2
changes:
- type: bug
text: "Clear pending heartbeat delay timer when presence effect is aborted, so destroy no longer waits for the full heartbeat interval."
- type: improvement
text: "Drop `delay` from event engine dependencies and client setup, as heartbeat delay now uses `heartbeatDelay`."
- type: improvement
text: "Add tests for timer cancel on destroy, no heartbeats after destroy and heartbeat loop stop on zero interval."
- date: 2026-10-13
version: v13.1.1
changes:
Expand Down Expand Up @@ -1513,7 +1522,7 @@ supported-platforms:
- 'Ubuntu 14.04 and up'
- 'Windows 7 and up'
version: 'Pubnub Javascript for Node'
version: '13.1.1'
version: '13.1.2'
sdks:
- full-name: PubNub Javascript SDK
short-name: Javascript
Expand All @@ -1529,7 +1538,7 @@ sdks:
- distribution-type: source
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/archive/refs/tags/v13.1.1.zip
location: https://github.com/pubnub/javascript/archive/refs/tags/v13.1.2.zip
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down Expand Up @@ -2200,7 +2209,7 @@ sdks:
- distribution-type: library
distribution-repository: GitHub release
package-name: pubnub.js
location: https://github.com/pubnub/javascript/releases/download/v13.1.1/pubnub.13.1.1.js
location: https://github.com/pubnub/javascript/releases/download/v13.1.2/pubnub.13.1.2.js
requires:
- name: 'agentkeepalive'
min-version: '3.5.2'
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## v13.1.2
October 14 2026

#### Fixed
- Clear pending heartbeat delay timer when presence effect is aborted, so destroy no longer waits for the full heartbeat interval.

#### Modified
- Drop `delay` from event engine dependencies and client setup, as heartbeat delay now uses `heartbeatDelay`.
- Add tests for timer cancel on destroy, no heartbeats after destroy and heartbeat loop stop on zero interval.

## v13.1.1
October 13 2026

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ Watch [Getting Started with PubNub JS SDK](https://app.dashcam.io/replay/64ee0d2
npm install pubnub
```
* or download one of our builds from our CDN:
* https://cdn.pubnub.com/sdk/javascript/pubnub.13.1.1.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.13.1.1.min.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.13.1.2.js
* https://cdn.pubnub.com/sdk/javascript/pubnub.13.1.2.min.js

> **React Native:** the SDK relies on a global `URL` implementation, which React Native does not
> provide, so it depends on [`react-native-url-polyfill`](https://www.npmjs.com/package/react-native-url-polyfill).
Expand Down
33 changes: 23 additions & 10 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5613,7 +5613,7 @@
return base.PubNubFile;
},
get version() {
return '13.1.1';
return '13.1.2';
},
getVersion() {
return this.version;
Expand Down Expand Up @@ -9203,8 +9203,11 @@
}
})));
this.on(wait.type, asyncHandler((_1, abortSignal_1, _a) => __awaiter(this, [_1, abortSignal_1, _a], void 0, function* (_, abortSignal, { heartbeatDelay }) {
const { cancelDelay, promise } = heartbeatDelay();
abortSignal.throwIfAborted();
yield heartbeatDelay();
const unsubscribeAbortListener = abortSignal.subscribe(() => cancelDelay());
yield promise;
unsubscribeAbortListener();
abortSignal.throwIfAborted();
return engine.transition(timesUp());
})));
Expand Down Expand Up @@ -18607,13 +18610,24 @@
}));
this.makeUnsubscribe(parameters, () => { });
},
heartbeatDelay: () => new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new PubNubError('Heartbeat interval has been reset.'));
else
setTimeout(resolve, heartbeatInterval * 1000);
}),
heartbeatDelay: () => {
let cancelDelay = () => { };
const promise = new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new PubNubError('Heartbeat interval has been reset.'));
else {
const timeoutId = setTimeout(() => {
resolve();
}, heartbeatInterval * 1000);
cancelDelay = () => {
clearTimeout(timeoutId);
reject(new PubNubError('Heartbeat interval has been cancelled.'));
};
}
});
return { cancelDelay, promise };
},
emitStatus: (status) => this.emitStatus(status),
config: this._configuration,
presenceState: this.presenceState,
Expand All @@ -18639,7 +18653,6 @@
}));
return this.subscribeReceiveMessages(parameters);
},
delay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
join: (parameters) => {
var _a, _b;
this.logger.trace('EventEngine', () => ({
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/components/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const makeConfiguration = (base, setupCryptoModule) => {
return base.PubNubFile;
},
get version() {
return '13.1.1';
return '13.1.2';
},
getVersion() {
return this.version;
Expand Down
26 changes: 18 additions & 8 deletions lib/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,24 @@ class PubNubCore {
}));
this.makeUnsubscribe(parameters, () => { });
},
heartbeatDelay: () => new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new pubnub_error_1.PubNubError('Heartbeat interval has been reset.'));
else
setTimeout(resolve, heartbeatInterval * 1000);
}),
heartbeatDelay: () => {
let cancelDelay = () => { };
const promise = new Promise((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval)
reject(new pubnub_error_1.PubNubError('Heartbeat interval has been reset.'));
else {
const timeoutId = setTimeout(() => {
resolve();
}, heartbeatInterval * 1000);
cancelDelay = () => {
clearTimeout(timeoutId);
reject(new pubnub_error_1.PubNubError('Heartbeat interval has been cancelled.'));
};
}
});
return { cancelDelay, promise };
},
emitStatus: (status) => this.emitStatus(status),
config: this._configuration,
presenceState: this.presenceState,
Expand All @@ -276,7 +287,6 @@ class PubNubCore {
}));
return this.subscribeReceiveMessages(parameters);
},
delay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
join: (parameters) => {
var _a, _b;
this.logger.trace('EventEngine', () => ({
Expand Down
5 changes: 4 additions & 1 deletion lib/event-engine/presence/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ class PresenceEventEngineDispatcher extends core_1.Dispatcher {
}
})));
this.on(effects.wait.type, (0, core_1.asyncHandler)((_1, abortSignal_1, _a) => __awaiter(this, [_1, abortSignal_1, _a], void 0, function* (_, abortSignal, { heartbeatDelay }) {
const { cancelDelay, promise } = heartbeatDelay();
abortSignal.throwIfAborted();
yield heartbeatDelay();
const unsubscribeAbortListener = abortSignal.subscribe(() => cancelDelay());
yield promise;
unsubscribeAbortListener();
abortSignal.throwIfAborted();
return engine.transition(events.timesUp());
})));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pubnub",
"version": "13.1.1",
"version": "13.1.2",
"author": "PubNub <support@pubnub.com>",
"description": "PubNub SDK for JavaScript. Publish/subscribe messaging over HTTP long-poll with server-assigned message ordering, channel occupancy via Here Now, and message history through Message Persistence.",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/components/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export const makeConfiguration = (
return base.PubNubFile;
},
get version(): string {
return '13.1.1';
return '13.1.2';
},
getVersion(): string {
return this.version;
Expand Down
21 changes: 16 additions & 5 deletions src/core/pubnub-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,24 @@ export class PubNubCore<

this.makeUnsubscribe(parameters, () => {});
},
heartbeatDelay: () =>
new Promise((resolve, reject) => {
heartbeatDelay: () => {
let cancelDelay = () => {};
const promise = new Promise<void>((resolve, reject) => {
heartbeatInterval = this._configuration.getHeartbeatInterval();
if (!heartbeatInterval) reject(new PubNubError('Heartbeat interval has been reset.'));
else setTimeout(resolve, heartbeatInterval * 1000);
}),
else {
const timeoutId = setTimeout(() => {
resolve();
}, heartbeatInterval * 1000);

cancelDelay = () => {
clearTimeout(timeoutId);
reject(new PubNubError('Heartbeat interval has been cancelled.'));
};
}
});
return { cancelDelay, promise };
},
emitStatus: (status) => this.emitStatus(status),
config: this._configuration,
presenceState: this.presenceState,
Expand Down Expand Up @@ -529,7 +541,6 @@ export class PubNubCore<

return this.subscribeReceiveMessages(parameters);
},
delay: (amount) => new Promise((resolve) => setTimeout(resolve, amount)),
join: (parameters) => {
this.logger.trace('EventEngine', () => ({
messageType: 'object',
Expand Down
2 changes: 0 additions & 2 deletions src/event-engine/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export type Dependencies = {
presenceState: Record<string, Payload>;
config: PrivateClientConfiguration;

delay: (milliseconds: number) => Promise<void>;

emitMessages: (
cursor: Subscription.SubscriptionCursor,
events: Subscription.SubscriptionResponse['messages'],
Expand Down
8 changes: 6 additions & 2 deletions src/event-engine/presence/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type Dependencies = {
callback?: ResultCallback<Presence.PresenceHeartbeatResponse>,
) => Promise<Presence.PresenceHeartbeatResponse | void>;
leave: (parameters: Presence.PresenceLeaveParameters) => void;
heartbeatDelay: () => Promise<void>;
heartbeatDelay: () => { cancelDelay: () => void; promise: Promise<void> };

config: PrivateClientConfiguration;
presenceState: Record<string, Payload>;
Expand Down Expand Up @@ -85,9 +85,13 @@ export class PresenceEventEngineDispatcher extends Dispatcher<effects.Effects, D
this.on(
effects.wait.type,
asyncHandler(async (_, abortSignal, { heartbeatDelay }) => {
const { cancelDelay, promise } = heartbeatDelay();
abortSignal.throwIfAborted();
await heartbeatDelay();
const unsubscribeAbortListener = abortSignal.subscribe(() => cancelDelay());

await promise;

unsubscribeAbortListener();
abortSignal.throwIfAborted();

return engine.transition(events.timesUp());
Expand Down
96 changes: 96 additions & 0 deletions test/unit/event_engine_presence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,100 @@ describe('EventEngine', () => {
expect(presenceEngine?.channels).to.deep.equal([]);
expect(presenceEngine?.groups).to.deep.equal([]);
});

describe('heartbeat delay timer', () => {
const sleep = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));

it('should cancel the pending heartbeat delay timer when destroyed', async () => {
utils
.createNock()
.get('/v2/presence/sub-key/demo/channel/test/heartbeat')
.query(true)
.reply(200, '{"message":"OK", "service":"Presence"}', { 'content-type': 'text/javascript' })
.persist();

// Unique delay identifies the heartbeat delay timer among other timers.
const heartbeatDelay = 7000;
pubnub.setHeartbeatInterval(heartbeatDelay / 1000);

const { setTimeout: scheduleTimeout, clearTimeout: cancelTimeout } = global;
const delayTimers: unknown[] = [];
const cancelledTimers = new Set<unknown>();
global.setTimeout = ((callback: () => void, delay?: number, ...args: unknown[]) => {
const timer = scheduleTimeout(callback, delay, ...args);
if (delay === heartbeatDelay) delayTimers.push(timer);
return timer;
}) as typeof setTimeout;
global.clearTimeout = ((timer: unknown) => {
cancelledTimers.add(timer);
cancelTimeout(timer as NodeJS.Timeout);
}) as typeof clearTimeout;

try {
// @ts-expect-error Intentional access to the private interface.
pubnub.join({ channels: ['test'] });

await forState('HEARTBEAT_COOLDOWN', 1000);
expect(delayTimers).to.have.lengthOf(1);

pubnub.destroy(true);
// Cancellation is dispatched asynchronously (`AbortSignal` notifies listeners on the next tick).
await sleep(50);

expect(cancelledTimers.has(delayTimers[0])).to.equal(true);
} finally {
global.setTimeout = scheduleTimeout;
global.clearTimeout = cancelTimeout;
}
});

it('should not send heartbeats after destroy', async () => {
let heartbeats = 0;
utils
.createNock()
.get('/v2/presence/sub-key/demo/channel/test/heartbeat')
.query(true)
.reply(() => {
heartbeats++;
return [200, '{"message":"OK", "service":"Presence"}', { 'content-type': 'text/javascript' }];
})
.persist();

// @ts-expect-error Intentional access to the private interface.
pubnub.join({ channels: ['test'] });

await forState('HEARTBEAT_COOLDOWN', 1000);
pubnub.destroy(true);
const heartbeatsAtDestroy = heartbeats;

await sleep(1500);

expect(heartbeats).to.equal(heartbeatsAtDestroy);
});

it('should stop the heartbeat loop when heartbeat interval is reset to zero', async () => {
let heartbeats = 0;
utils
.createNock()
.get('/v2/presence/sub-key/demo/channel/test/heartbeat')
.query(true)
.reply(() => {
heartbeats++;
return [200, '{"message":"OK", "service":"Presence"}', { 'content-type': 'text/javascript' }];
})
.persist();

// @ts-expect-error Intentional access to the private interface.
pubnub.join({ channels: ['test'] });

await forState('HEARTBEAT_COOLDOWN', 1000);
pubnub.setHeartbeatInterval(0);
const heartbeatsAtReset = heartbeats;

// At most one heartbeat already scheduled with the previous interval is expected after the reset.
await sleep(2500);

expect(heartbeats - heartbeatsAtReset).to.be.at.most(1);
});
});
});
Loading