@scalemule/sdk 0.0.61 → 0.0.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -2011,7 +2011,11 @@ declare class RealtimeService extends ServiceModule {
2011
2011
  private reconnectAttempt;
2012
2012
  private reconnectTimer;
2013
2013
  private heartbeatTimer;
2014
+ private connectWatchdogTimer;
2014
2015
  private authenticated;
2016
+ private lastInboundAt;
2017
+ private wakeHandler;
2018
+ private visibilityHandler;
2015
2019
  /** Current connection status */
2016
2020
  get status(): ConnectionStatus;
2017
2021
  /**
@@ -2051,10 +2055,23 @@ declare class RealtimeService extends ServiceModule {
2051
2055
  private dispatchPresence;
2052
2056
  private sendWs;
2053
2057
  private scheduleReconnect;
2058
+ /** Drop the current socket without triggering its onclose reconnect path. */
2059
+ private teardownSocket;
2060
+ /** Tear down a dead-but-open socket and reconnect. */
2061
+ private forceReconnect;
2062
+ /**
2063
+ * Browsers suspend timers and silently kill sockets during sleep or network
2064
+ * changes. When the page wakes (tab visible, window focused, network back),
2065
+ * verify the connection instead of waiting for the next heartbeat tick.
2066
+ */
2067
+ private installWakeListeners;
2068
+ private removeWakeListeners;
2069
+ private handleWake;
2054
2070
  private getReconnectDelay;
2055
2071
  private startHeartbeat;
2056
2072
  private clearHeartbeat;
2057
2073
  private clearTimers;
2074
+ private clearConnectWatchdog;
2058
2075
  private setStatus;
2059
2076
  }
2060
2077
 
package/dist/index.d.ts CHANGED
@@ -2011,7 +2011,11 @@ declare class RealtimeService extends ServiceModule {
2011
2011
  private reconnectAttempt;
2012
2012
  private reconnectTimer;
2013
2013
  private heartbeatTimer;
2014
+ private connectWatchdogTimer;
2014
2015
  private authenticated;
2016
+ private lastInboundAt;
2017
+ private wakeHandler;
2018
+ private visibilityHandler;
2015
2019
  /** Current connection status */
2016
2020
  get status(): ConnectionStatus;
2017
2021
  /**
@@ -2051,10 +2055,23 @@ declare class RealtimeService extends ServiceModule {
2051
2055
  private dispatchPresence;
2052
2056
  private sendWs;
2053
2057
  private scheduleReconnect;
2058
+ /** Drop the current socket without triggering its onclose reconnect path. */
2059
+ private teardownSocket;
2060
+ /** Tear down a dead-but-open socket and reconnect. */
2061
+ private forceReconnect;
2062
+ /**
2063
+ * Browsers suspend timers and silently kill sockets during sleep or network
2064
+ * changes. When the page wakes (tab visible, window focused, network back),
2065
+ * verify the connection instead of waiting for the next heartbeat tick.
2066
+ */
2067
+ private installWakeListeners;
2068
+ private removeWakeListeners;
2069
+ private handleWake;
2054
2070
  private getReconnectDelay;
2055
2071
  private startHeartbeat;
2056
2072
  private clearHeartbeat;
2057
2073
  private clearTimers;
2074
+ private clearConnectWatchdog;
2058
2075
  private setStatus;
2059
2076
  }
2060
2077
 
package/dist/index.js CHANGED
@@ -1152,7 +1152,7 @@ var ScaleMuleClient = class {
1152
1152
  const method = (init.method || "GET").toUpperCase();
1153
1153
  const timeout = init.timeout || this.defaultTimeout;
1154
1154
  const maxRetries = init.skipRetry ? 0 : init.retries ?? this.maxRetries;
1155
- const bodyStr = init.body ? JSON.stringify(init.body) : void 0;
1155
+ const bodyStr = init.body !== void 0 && init.body !== null ? typeof init.body === "string" ? init.body : JSON.stringify(init.body) : void 0;
1156
1156
  let lastError = null;
1157
1157
  let idempotencyKey = null;
1158
1158
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
@@ -1161,6 +1161,9 @@ var ScaleMuleClient = class {
1161
1161
  "User-Agent": `ScaleMule-SDK-TypeScript/${SDK_VERSION}`,
1162
1162
  ...init.headers
1163
1163
  };
1164
+ if (bodyStr !== void 0 && !Object.keys(headers).some((k) => k.toLowerCase() === "content-type")) {
1165
+ headers["Content-Type"] = "application/json";
1166
+ }
1164
1167
  if (!init.skipAuth && this.sessionToken) {
1165
1168
  headers["Authorization"] = `Bearer ${this.sessionToken}`;
1166
1169
  }
@@ -4149,6 +4152,9 @@ function sleep3(ms) {
4149
4152
  var DEFAULT_RECONNECT_BASE_MS = 1e3;
4150
4153
  var MAX_RECONNECT_MS = 3e4;
4151
4154
  var HEARTBEAT_INTERVAL_MS = 3e4;
4155
+ var TICKET_FETCH_TIMEOUT_MS = 1e4;
4156
+ var CONNECT_WATCHDOG_MS = 2e4;
4157
+ var LIVENESS_TIMEOUT_MS = 75e3;
4152
4158
  var RealtimeService = class extends ServiceModule {
4153
4159
  constructor() {
4154
4160
  super(...arguments);
@@ -4162,7 +4168,11 @@ var RealtimeService = class extends ServiceModule {
4162
4168
  this.reconnectAttempt = 0;
4163
4169
  this.reconnectTimer = null;
4164
4170
  this.heartbeatTimer = null;
4171
+ this.connectWatchdogTimer = null;
4165
4172
  this.authenticated = false;
4173
+ this.lastInboundAt = 0;
4174
+ this.wakeHandler = null;
4175
+ this.visibilityHandler = null;
4166
4176
  }
4167
4177
  /** Current connection status */
4168
4178
  get status() {
@@ -4266,6 +4276,7 @@ var RealtimeService = class extends ServiceModule {
4266
4276
  /** Disconnect and clean up all subscriptions. */
4267
4277
  disconnect() {
4268
4278
  this.clearTimers();
4279
+ this.removeWakeListeners();
4269
4280
  this.subscriptions.clear();
4270
4281
  this.presenceCallbacks.clear();
4271
4282
  this.statusCallbacks.clear();
@@ -4284,6 +4295,15 @@ var RealtimeService = class extends ServiceModule {
4284
4295
  connect() {
4285
4296
  if (this._status === "connecting" || this._status === "connected") return;
4286
4297
  this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
4298
+ this.installWakeListeners();
4299
+ this.clearConnectWatchdog();
4300
+ this.connectWatchdogTimer = setTimeout(() => {
4301
+ this.connectWatchdogTimer = null;
4302
+ if (this._status !== "connected") {
4303
+ this.teardownSocket();
4304
+ this.scheduleReconnect();
4305
+ }
4306
+ }, CONNECT_WATCHDOG_MS);
4287
4307
  this.fetchTicketAndConnect();
4288
4308
  }
4289
4309
  async fetchTicketAndConnect() {
@@ -4294,10 +4314,18 @@ var RealtimeService = class extends ServiceModule {
4294
4314
  if (apiKey) headers["x-api-key"] = apiKey;
4295
4315
  const token = this.client.getSessionToken();
4296
4316
  if (token) headers["Authorization"] = `Bearer ${token}`;
4297
- const ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
4298
- method: "POST",
4299
- headers
4300
- });
4317
+ const abort = new AbortController();
4318
+ const abortTimer = setTimeout(() => abort.abort(), TICKET_FETCH_TIMEOUT_MS);
4319
+ let ticketRes;
4320
+ try {
4321
+ ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
4322
+ method: "POST",
4323
+ headers,
4324
+ signal: abort.signal
4325
+ });
4326
+ } finally {
4327
+ clearTimeout(abortTimer);
4328
+ }
4301
4329
  let wsUrl;
4302
4330
  if (ticketRes.ok) {
4303
4331
  const ticketData = await ticketRes.json();
@@ -4315,6 +4343,7 @@ var RealtimeService = class extends ServiceModule {
4315
4343
  }
4316
4344
  this.ws.onopen = () => {
4317
4345
  this.reconnectAttempt = 0;
4346
+ this.lastInboundAt = Date.now();
4318
4347
  if (!this.usedTicketAuth) {
4319
4348
  this.authenticate();
4320
4349
  }
@@ -4332,6 +4361,7 @@ var RealtimeService = class extends ServiceModule {
4332
4361
  }
4333
4362
  };
4334
4363
  this.ws.onmessage = (event) => {
4364
+ this.lastInboundAt = Date.now();
4335
4365
  try {
4336
4366
  const msg = JSON.parse(event.data);
4337
4367
  this.handleMessage(msg);
@@ -4372,6 +4402,10 @@ var RealtimeService = class extends ServiceModule {
4372
4402
  break;
4373
4403
  case "error":
4374
4404
  break;
4405
+ case "reconnect":
4406
+ this.reconnectAttempt = 0;
4407
+ this.forceReconnect();
4408
+ break;
4375
4409
  case "presence_state":
4376
4410
  this.dispatchPresence({
4377
4411
  type: "state",
@@ -4426,6 +4460,7 @@ var RealtimeService = class extends ServiceModule {
4426
4460
  // Private: Reconnection
4427
4461
  // --------------------------------------------------------------------------
4428
4462
  scheduleReconnect() {
4463
+ this.clearConnectWatchdog();
4429
4464
  if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) {
4430
4465
  this.setStatus("disconnected");
4431
4466
  return;
@@ -4433,10 +4468,83 @@ var RealtimeService = class extends ServiceModule {
4433
4468
  this.setStatus("reconnecting");
4434
4469
  const delay = this.getReconnectDelay();
4435
4470
  this.reconnectTimer = setTimeout(() => {
4471
+ this.reconnectTimer = null;
4436
4472
  this.reconnectAttempt++;
4437
4473
  this.connect();
4438
4474
  }, delay);
4439
4475
  }
4476
+ /** Drop the current socket without triggering its onclose reconnect path. */
4477
+ teardownSocket() {
4478
+ if (this.ws) {
4479
+ this.ws.onclose = null;
4480
+ this.ws.onerror = null;
4481
+ try {
4482
+ this.ws.close();
4483
+ } catch {
4484
+ }
4485
+ this.ws = null;
4486
+ }
4487
+ this.authenticated = false;
4488
+ this.clearHeartbeat();
4489
+ }
4490
+ /** Tear down a dead-but-open socket and reconnect. */
4491
+ forceReconnect() {
4492
+ this.teardownSocket();
4493
+ this.scheduleReconnect();
4494
+ }
4495
+ // --------------------------------------------------------------------------
4496
+ // Private: Wake / network-change recovery
4497
+ // --------------------------------------------------------------------------
4498
+ /**
4499
+ * Browsers suspend timers and silently kill sockets during sleep or network
4500
+ * changes. When the page wakes (tab visible, window focused, network back),
4501
+ * verify the connection instead of waiting for the next heartbeat tick.
4502
+ */
4503
+ installWakeListeners() {
4504
+ if (this.wakeHandler || typeof window === "undefined") return;
4505
+ this.wakeHandler = () => this.handleWake();
4506
+ window.addEventListener("online", this.wakeHandler);
4507
+ window.addEventListener("focus", this.wakeHandler);
4508
+ if (typeof document !== "undefined") {
4509
+ this.visibilityHandler = () => {
4510
+ if (document.visibilityState === "visible") this.handleWake();
4511
+ };
4512
+ document.addEventListener("visibilitychange", this.visibilityHandler);
4513
+ }
4514
+ }
4515
+ removeWakeListeners() {
4516
+ if (typeof window !== "undefined" && this.wakeHandler) {
4517
+ window.removeEventListener("online", this.wakeHandler);
4518
+ window.removeEventListener("focus", this.wakeHandler);
4519
+ }
4520
+ if (typeof document !== "undefined" && this.visibilityHandler) {
4521
+ document.removeEventListener("visibilitychange", this.visibilityHandler);
4522
+ }
4523
+ this.wakeHandler = null;
4524
+ this.visibilityHandler = null;
4525
+ }
4526
+ handleWake() {
4527
+ if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) return;
4528
+ if (this._status === "connected") {
4529
+ if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
4530
+ this.reconnectAttempt = 0;
4531
+ this.forceReconnect();
4532
+ } else if (this.ws?.readyState === WebSocket.OPEN) {
4533
+ this.ws.send("ping");
4534
+ }
4535
+ return;
4536
+ }
4537
+ if (this._status === "reconnecting" && this.reconnectTimer) {
4538
+ clearTimeout(this.reconnectTimer);
4539
+ this.reconnectTimer = null;
4540
+ this.reconnectAttempt = 0;
4541
+ this.connect();
4542
+ return;
4543
+ }
4544
+ if (this._status === "disconnected") {
4545
+ this.connect();
4546
+ }
4547
+ }
4440
4548
  getReconnectDelay() {
4441
4549
  const exponential = DEFAULT_RECONNECT_BASE_MS * Math.pow(2, this.reconnectAttempt);
4442
4550
  const jitter = Math.random() * 0.3 * exponential;
@@ -4448,6 +4556,10 @@ var RealtimeService = class extends ServiceModule {
4448
4556
  startHeartbeat() {
4449
4557
  this.clearHeartbeat();
4450
4558
  this.heartbeatTimer = setInterval(() => {
4559
+ if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
4560
+ this.forceReconnect();
4561
+ return;
4562
+ }
4451
4563
  if (this.ws?.readyState === WebSocket.OPEN) {
4452
4564
  this.ws.send("ping");
4453
4565
  }
@@ -4461,17 +4573,27 @@ var RealtimeService = class extends ServiceModule {
4461
4573
  }
4462
4574
  clearTimers() {
4463
4575
  this.clearHeartbeat();
4576
+ this.clearConnectWatchdog();
4464
4577
  if (this.reconnectTimer) {
4465
4578
  clearTimeout(this.reconnectTimer);
4466
4579
  this.reconnectTimer = null;
4467
4580
  }
4468
4581
  }
4582
+ clearConnectWatchdog() {
4583
+ if (this.connectWatchdogTimer) {
4584
+ clearTimeout(this.connectWatchdogTimer);
4585
+ this.connectWatchdogTimer = null;
4586
+ }
4587
+ }
4469
4588
  // --------------------------------------------------------------------------
4470
4589
  // Private: Status
4471
4590
  // --------------------------------------------------------------------------
4472
4591
  setStatus(status) {
4473
4592
  if (this._status === status) return;
4474
4593
  this._status = status;
4594
+ if (status === "connected") {
4595
+ this.clearConnectWatchdog();
4596
+ }
4475
4597
  for (const cb of this.statusCallbacks) {
4476
4598
  try {
4477
4599
  cb(status);
package/dist/index.mjs CHANGED
@@ -1048,7 +1048,7 @@ var ScaleMuleClient = class {
1048
1048
  const method = (init.method || "GET").toUpperCase();
1049
1049
  const timeout = init.timeout || this.defaultTimeout;
1050
1050
  const maxRetries = init.skipRetry ? 0 : init.retries ?? this.maxRetries;
1051
- const bodyStr = init.body ? JSON.stringify(init.body) : void 0;
1051
+ const bodyStr = init.body !== void 0 && init.body !== null ? typeof init.body === "string" ? init.body : JSON.stringify(init.body) : void 0;
1052
1052
  let lastError = null;
1053
1053
  let idempotencyKey = null;
1054
1054
  for (let attempt = 0; attempt <= maxRetries; attempt++) {
@@ -1057,6 +1057,9 @@ var ScaleMuleClient = class {
1057
1057
  "User-Agent": `ScaleMule-SDK-TypeScript/${SDK_VERSION}`,
1058
1058
  ...init.headers
1059
1059
  };
1060
+ if (bodyStr !== void 0 && !Object.keys(headers).some((k) => k.toLowerCase() === "content-type")) {
1061
+ headers["Content-Type"] = "application/json";
1062
+ }
1060
1063
  if (!init.skipAuth && this.sessionToken) {
1061
1064
  headers["Authorization"] = `Bearer ${this.sessionToken}`;
1062
1065
  }
@@ -4045,6 +4048,9 @@ function sleep3(ms) {
4045
4048
  var DEFAULT_RECONNECT_BASE_MS = 1e3;
4046
4049
  var MAX_RECONNECT_MS = 3e4;
4047
4050
  var HEARTBEAT_INTERVAL_MS = 3e4;
4051
+ var TICKET_FETCH_TIMEOUT_MS = 1e4;
4052
+ var CONNECT_WATCHDOG_MS = 2e4;
4053
+ var LIVENESS_TIMEOUT_MS = 75e3;
4048
4054
  var RealtimeService = class extends ServiceModule {
4049
4055
  constructor() {
4050
4056
  super(...arguments);
@@ -4058,7 +4064,11 @@ var RealtimeService = class extends ServiceModule {
4058
4064
  this.reconnectAttempt = 0;
4059
4065
  this.reconnectTimer = null;
4060
4066
  this.heartbeatTimer = null;
4067
+ this.connectWatchdogTimer = null;
4061
4068
  this.authenticated = false;
4069
+ this.lastInboundAt = 0;
4070
+ this.wakeHandler = null;
4071
+ this.visibilityHandler = null;
4062
4072
  }
4063
4073
  /** Current connection status */
4064
4074
  get status() {
@@ -4162,6 +4172,7 @@ var RealtimeService = class extends ServiceModule {
4162
4172
  /** Disconnect and clean up all subscriptions. */
4163
4173
  disconnect() {
4164
4174
  this.clearTimers();
4175
+ this.removeWakeListeners();
4165
4176
  this.subscriptions.clear();
4166
4177
  this.presenceCallbacks.clear();
4167
4178
  this.statusCallbacks.clear();
@@ -4180,6 +4191,15 @@ var RealtimeService = class extends ServiceModule {
4180
4191
  connect() {
4181
4192
  if (this._status === "connecting" || this._status === "connected") return;
4182
4193
  this.setStatus(this.reconnectAttempt > 0 ? "reconnecting" : "connecting");
4194
+ this.installWakeListeners();
4195
+ this.clearConnectWatchdog();
4196
+ this.connectWatchdogTimer = setTimeout(() => {
4197
+ this.connectWatchdogTimer = null;
4198
+ if (this._status !== "connected") {
4199
+ this.teardownSocket();
4200
+ this.scheduleReconnect();
4201
+ }
4202
+ }, CONNECT_WATCHDOG_MS);
4183
4203
  this.fetchTicketAndConnect();
4184
4204
  }
4185
4205
  async fetchTicketAndConnect() {
@@ -4190,10 +4210,18 @@ var RealtimeService = class extends ServiceModule {
4190
4210
  if (apiKey) headers["x-api-key"] = apiKey;
4191
4211
  const token = this.client.getSessionToken();
4192
4212
  if (token) headers["Authorization"] = `Bearer ${token}`;
4193
- const ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
4194
- method: "POST",
4195
- headers
4196
- });
4213
+ const abort = new AbortController();
4214
+ const abortTimer = setTimeout(() => abort.abort(), TICKET_FETCH_TIMEOUT_MS);
4215
+ let ticketRes;
4216
+ try {
4217
+ ticketRes = await fetch(`${baseUrl}/v1/realtime/ws/ticket`, {
4218
+ method: "POST",
4219
+ headers,
4220
+ signal: abort.signal
4221
+ });
4222
+ } finally {
4223
+ clearTimeout(abortTimer);
4224
+ }
4197
4225
  let wsUrl;
4198
4226
  if (ticketRes.ok) {
4199
4227
  const ticketData = await ticketRes.json();
@@ -4211,6 +4239,7 @@ var RealtimeService = class extends ServiceModule {
4211
4239
  }
4212
4240
  this.ws.onopen = () => {
4213
4241
  this.reconnectAttempt = 0;
4242
+ this.lastInboundAt = Date.now();
4214
4243
  if (!this.usedTicketAuth) {
4215
4244
  this.authenticate();
4216
4245
  }
@@ -4228,6 +4257,7 @@ var RealtimeService = class extends ServiceModule {
4228
4257
  }
4229
4258
  };
4230
4259
  this.ws.onmessage = (event) => {
4260
+ this.lastInboundAt = Date.now();
4231
4261
  try {
4232
4262
  const msg = JSON.parse(event.data);
4233
4263
  this.handleMessage(msg);
@@ -4268,6 +4298,10 @@ var RealtimeService = class extends ServiceModule {
4268
4298
  break;
4269
4299
  case "error":
4270
4300
  break;
4301
+ case "reconnect":
4302
+ this.reconnectAttempt = 0;
4303
+ this.forceReconnect();
4304
+ break;
4271
4305
  case "presence_state":
4272
4306
  this.dispatchPresence({
4273
4307
  type: "state",
@@ -4322,6 +4356,7 @@ var RealtimeService = class extends ServiceModule {
4322
4356
  // Private: Reconnection
4323
4357
  // --------------------------------------------------------------------------
4324
4358
  scheduleReconnect() {
4359
+ this.clearConnectWatchdog();
4325
4360
  if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) {
4326
4361
  this.setStatus("disconnected");
4327
4362
  return;
@@ -4329,10 +4364,83 @@ var RealtimeService = class extends ServiceModule {
4329
4364
  this.setStatus("reconnecting");
4330
4365
  const delay = this.getReconnectDelay();
4331
4366
  this.reconnectTimer = setTimeout(() => {
4367
+ this.reconnectTimer = null;
4332
4368
  this.reconnectAttempt++;
4333
4369
  this.connect();
4334
4370
  }, delay);
4335
4371
  }
4372
+ /** Drop the current socket without triggering its onclose reconnect path. */
4373
+ teardownSocket() {
4374
+ if (this.ws) {
4375
+ this.ws.onclose = null;
4376
+ this.ws.onerror = null;
4377
+ try {
4378
+ this.ws.close();
4379
+ } catch {
4380
+ }
4381
+ this.ws = null;
4382
+ }
4383
+ this.authenticated = false;
4384
+ this.clearHeartbeat();
4385
+ }
4386
+ /** Tear down a dead-but-open socket and reconnect. */
4387
+ forceReconnect() {
4388
+ this.teardownSocket();
4389
+ this.scheduleReconnect();
4390
+ }
4391
+ // --------------------------------------------------------------------------
4392
+ // Private: Wake / network-change recovery
4393
+ // --------------------------------------------------------------------------
4394
+ /**
4395
+ * Browsers suspend timers and silently kill sockets during sleep or network
4396
+ * changes. When the page wakes (tab visible, window focused, network back),
4397
+ * verify the connection instead of waiting for the next heartbeat tick.
4398
+ */
4399
+ installWakeListeners() {
4400
+ if (this.wakeHandler || typeof window === "undefined") return;
4401
+ this.wakeHandler = () => this.handleWake();
4402
+ window.addEventListener("online", this.wakeHandler);
4403
+ window.addEventListener("focus", this.wakeHandler);
4404
+ if (typeof document !== "undefined") {
4405
+ this.visibilityHandler = () => {
4406
+ if (document.visibilityState === "visible") this.handleWake();
4407
+ };
4408
+ document.addEventListener("visibilitychange", this.visibilityHandler);
4409
+ }
4410
+ }
4411
+ removeWakeListeners() {
4412
+ if (typeof window !== "undefined" && this.wakeHandler) {
4413
+ window.removeEventListener("online", this.wakeHandler);
4414
+ window.removeEventListener("focus", this.wakeHandler);
4415
+ }
4416
+ if (typeof document !== "undefined" && this.visibilityHandler) {
4417
+ document.removeEventListener("visibilitychange", this.visibilityHandler);
4418
+ }
4419
+ this.wakeHandler = null;
4420
+ this.visibilityHandler = null;
4421
+ }
4422
+ handleWake() {
4423
+ if (this.subscriptions.size === 0 && this.presenceCallbacks.size === 0) return;
4424
+ if (this._status === "connected") {
4425
+ if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
4426
+ this.reconnectAttempt = 0;
4427
+ this.forceReconnect();
4428
+ } else if (this.ws?.readyState === WebSocket.OPEN) {
4429
+ this.ws.send("ping");
4430
+ }
4431
+ return;
4432
+ }
4433
+ if (this._status === "reconnecting" && this.reconnectTimer) {
4434
+ clearTimeout(this.reconnectTimer);
4435
+ this.reconnectTimer = null;
4436
+ this.reconnectAttempt = 0;
4437
+ this.connect();
4438
+ return;
4439
+ }
4440
+ if (this._status === "disconnected") {
4441
+ this.connect();
4442
+ }
4443
+ }
4336
4444
  getReconnectDelay() {
4337
4445
  const exponential = DEFAULT_RECONNECT_BASE_MS * Math.pow(2, this.reconnectAttempt);
4338
4446
  const jitter = Math.random() * 0.3 * exponential;
@@ -4344,6 +4452,10 @@ var RealtimeService = class extends ServiceModule {
4344
4452
  startHeartbeat() {
4345
4453
  this.clearHeartbeat();
4346
4454
  this.heartbeatTimer = setInterval(() => {
4455
+ if (Date.now() - this.lastInboundAt > LIVENESS_TIMEOUT_MS) {
4456
+ this.forceReconnect();
4457
+ return;
4458
+ }
4347
4459
  if (this.ws?.readyState === WebSocket.OPEN) {
4348
4460
  this.ws.send("ping");
4349
4461
  }
@@ -4357,17 +4469,27 @@ var RealtimeService = class extends ServiceModule {
4357
4469
  }
4358
4470
  clearTimers() {
4359
4471
  this.clearHeartbeat();
4472
+ this.clearConnectWatchdog();
4360
4473
  if (this.reconnectTimer) {
4361
4474
  clearTimeout(this.reconnectTimer);
4362
4475
  this.reconnectTimer = null;
4363
4476
  }
4364
4477
  }
4478
+ clearConnectWatchdog() {
4479
+ if (this.connectWatchdogTimer) {
4480
+ clearTimeout(this.connectWatchdogTimer);
4481
+ this.connectWatchdogTimer = null;
4482
+ }
4483
+ }
4365
4484
  // --------------------------------------------------------------------------
4366
4485
  // Private: Status
4367
4486
  // --------------------------------------------------------------------------
4368
4487
  setStatus(status) {
4369
4488
  if (this._status === status) return;
4370
4489
  this._status = status;
4490
+ if (status === "connected") {
4491
+ this.clearConnectWatchdog();
4492
+ }
4371
4493
  for (const cb of this.statusCallbacks) {
4372
4494
  try {
4373
4495
  cb(status);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scalemule/sdk",
3
- "version": "0.0.61",
3
+ "version": "0.0.63",
4
4
  "description": "Official TypeScript/JavaScript SDK for ScaleMule Backend-as-a-Service",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",