Skip to content

Commit 3c6851c

Browse files
author
Javier Acuna
committed
Merge branch 'develop' into 'master'
Develop See merge request prey/js/prey-node-client!1315
2 parents e460724 + 5ce4df0 commit 3c6851c

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
## [v1.13.24](https://github.com/prey/prey-node-client/tree/v1.13.24) (2026-01-09)
4+
[Full Changelog](https://github.com/prey/prey-node-client/compare/v1.13.23..v1.13.24)
5+
6+
- Fix: Fixes an issue where WebSocket connections failed to reconnect after the system woke from sleep mode. ([SoraKenji](https://github.com/SoraKenji))
7+
38
## [v1.13.23](https://github.com/prey/prey-node-client/tree/v1.13.23) (2026-01-06)
49
[Full Changelog](https://github.com/prey/prey-node-client/compare/v1.13.22..v1.13.23)
510

lib/agent/control-panel/websockets/index.js

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ let countNotConnectionProxy = 0;
4242
let pongReceived = false;
4343
let timerSendLocation;
4444
let lastLocationTime;
45-
let reconnectAttempts = 0;
4645
let reconnectTimerId = null;
47-
const reconnectBaseDelay = 1000; // 1s
48-
const reconnectMaxDelay = 30000; // 30s
4946
let isReconnecting = false;
5047

5148
const startupTimeout = 5000;
@@ -235,6 +232,22 @@ const processNextMessage = () => {
235232
}
236233
};
237234

235+
const commandProcessedStoredFn = (commandId) => {
236+
if (commandIdToStructure.has(commandId)) {
237+
const structure = commandIdToStructure.get(commandId);
238+
logger.debug(`Command ${commandId} completed, releasing structure: ${structure}`);
239+
240+
// Clear the timeout since command completed successfully
241+
if (structureTimeouts.has(structure)) {
242+
clearTimeout(structureTimeouts.get(structure));
243+
structureTimeouts.delete(structure);
244+
}
245+
246+
processingStructures.delete(structure);
247+
commandIdToStructure.delete(commandId);
248+
}
249+
};
250+
238251
// Start the message processor
239252
const startMessageProcessor = () => {
240253
if (processorIntervalId) {
@@ -245,21 +258,7 @@ const startMessageProcessor = () => {
245258

246259
// Listen to command completion events to unblock structures
247260
if (hooks) {
248-
hooks.on('command_processed_or_stored', (commandId) => {
249-
if (commandIdToStructure.has(commandId)) {
250-
const structure = commandIdToStructure.get(commandId);
251-
logger.debug(`Command ${commandId} completed, releasing structure: ${structure}`);
252-
253-
// Clear the timeout since command completed successfully
254-
if (structureTimeouts.has(structure)) {
255-
clearTimeout(structureTimeouts.get(structure));
256-
structureTimeouts.delete(structure);
257-
}
258-
259-
processingStructures.delete(structure);
260-
commandIdToStructure.delete(commandId);
261-
}
262-
});
261+
hooks.on('command_processed_or_stored', commandProcessedStoredFn);
263262
}
264263

265264
processorIntervalId = setInterval(processNextMessage, MESSAGE_PROCESS_DELAY);
@@ -275,7 +274,7 @@ const stopMessageProcessor = () => {
275274

276275
// Clean up event listener
277276
if (hooks && typeof hooks.removeListener === 'function') {
278-
hooks.removeListener('command_processed_or_stored');
277+
hooks.removeListener('command_processed_or_stored', commandProcessedStoredFn);
279278
}
280279

281280
// Clear all pending timeouts
@@ -416,16 +415,14 @@ const validationConnectionsProxy = () => {
416415
};
417416

418417
const computeReconnectDelay = () => {
419-
const jitter = Math.floor(Math.random() * 1000);
420-
const delay = Math.min(reconnectBaseDelay * (2 ** reconnectAttempts) + jitter, reconnectMaxDelay);
421-
return delay;
418+
const jitter = Math.floor(Math.random() * 4000);
419+
return jitter;
422420
};
423421

424422
const scheduleReconnect = (reason, immediate = false) => {
425-
if (!exports.re_schedule) return;
423+
logger.info(`trying to reconnect with ${reason} and immediate=${immediate}`);
426424
if (isReconnecting) return;
427425
isReconnecting = true;
428-
reconnectAttempts += 1;
429426
clearAndResetIntervals();
430427
validationConnectionsProxy();
431428

@@ -455,7 +452,7 @@ const scheduleReconnect = (reason, immediate = false) => {
455452
logger.warn('isReconnecting stuck true, resetting');
456453
isReconnecting = false;
457454
}
458-
}, reconnectMaxDelay + 10000);
455+
}, 5000);
459456
};
460457

461458
const restartWebsocketCall = () => {
@@ -607,16 +604,15 @@ const webSocketSettings = () => {
607604
notifyAckInterval = setInterval(retryAckResponses, 4 * 1000);
608605
getStatusInterval = setInterval(getStatusByInterval, 5 * 60 * 1000);
609606
timeOutCancelIntervalHearBeat = setTimeout(() => {
610-
setIntervalWSStatus = setInterval(exports.heartbeat, 30 * 1000);
611-
}, 90 * 1000);
607+
setIntervalWSStatus = setInterval(exports.heartbeat, 10 * 1000);
608+
}, 60 * 1000);
612609
const proxy = config.getData('try_proxy');
613610
let protocol = config.getData('control-panel.protocol');
614611
const host = config.getData('control-panel.host');
615612
const deviceKey = keys.get().device;
616613
const apiKey = keys.get().api;
617614

618615
protocol = protocol === 'https' ? 'wss' : 'ws';
619-
620616
if (!keys.get().device) {
621617
propagateError(errors.get('NO_DEVICE_KEY'));
622618
exports.unload();
@@ -639,15 +635,16 @@ const webSocketSettings = () => {
639635
logger.info('Setting up proxy');
640636
}
641637
gettingStatus = true;
642-
if (!(!ws || !ws.readyState || ws.readyState !== 1)) ws.terminate();
638+
if (!(!ws || !ws.readyState || ws.readyState !== 1)) {
639+
ws.terminate();
640+
}
643641
statusTrigger.get_status((_err, status) => {
644642
gettingStatus = false;
645643
// Create websocket
646644
ws = new WebSocket(`${protocol}://${url}`, options);
647645
ws.on('open', () => {
648646
logger.debug('ws.open: connection established');
649647
// successful connect: reset reconnect attempts
650-
reconnectAttempts = 0;
651648
if (reconnectTimerId) {
652649
clearTimeout(reconnectTimerId);
653650
reconnectTimerId = null;
@@ -926,7 +923,6 @@ exports.load = (cb) => {
926923
exports.unload = (cb) => {
927924
logger.debug('exports.unload: shutting down websocket module');
928925
clearAndResetIntervals(true);
929-
exports.re_schedule = false;
930926
if (ws) ws.terminate();
931927
clearTimeout(pingTimeout);
932928
hooks.remove('connected');

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "prey",
3-
"version": "1.13.23",
3+
"version": "1.13.24",
44
"author": "Engineering <[email protected]>",
55
"keywords": [
66
"prey",

0 commit comments

Comments
 (0)