nodejs-insta-private-api-mqtt 1.3.21 → 1.3.22

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/mqtt-shim.js CHANGED
@@ -98,7 +98,7 @@ class MqttClient extends EventEmitter {
98
98
  this.packetIdCounter = 1;
99
99
  this.pendingPublishes = new Map(); // PacketID -> {resolve, reject, timer}
100
100
  this.pingTimer = null;
101
- this.keepAliveInterval = 60000;
101
+ this.keepAliveInterval = 30000;
102
102
  this.recvBuffer = Buffer.alloc(0); // buffer pentru date primite (concatenează fragmente)
103
103
  this._defaultReadMapInstalled = false;
104
104
 
@@ -158,7 +158,7 @@ class MqttClient extends EventEmitter {
158
158
  ...this.options,
159
159
  ...connectOpts,
160
160
  payload: this.connectPayload || connectOpts.payload,
161
- keepAlive: 60
161
+ keepAlive: 30
162
162
  });
163
163
  const packetData = writer.toBuffer();
164
164
  let remLen = packetData.length;
@@ -445,6 +445,81 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
445
445
 
446
446
  this.emit('connected');
447
447
 
448
+ // -------------------- WATCHDOG / KEEPALIVE / TRAFFIC MONITOR --------------------
449
+ // Track last message time for inactivity detection
450
+ try {
451
+ this._lastMessageAt = Date.now();
452
+
453
+ // update on common receive events
454
+ const updateLast = () => { try { this._lastMessageAt = Date.now(); } catch (e) {} };
455
+ this.on('receive', updateLast);
456
+ this.on('receiveRaw', updateLast);
457
+ this.on('message', updateLast);
458
+ this.on('iris', updateLast);
459
+
460
+ // Timings configurable via initOptions (fallback sensible defaults)
461
+ const KEEPALIVE_FOREGROUND_MS = (this.initOptions && this.initOptions.keepaliveForegroundMs) ? this.initOptions.keepaliveForegroundMs : 30000;
462
+ const MESSAGE_SYNC_REFRESH_MS = (this.initOptions && this.initOptions.messageSyncRefreshMs) ? this.initOptions.messageSyncRefreshMs : 60000;
463
+ const TRAFFIC_INACTIVITY_MS = (this.initOptions && this.initOptions.trafficInactivityMs) ? this.initOptions.trafficInactivityMs : 90000;
464
+
465
+ // Foreground keepalive: tell server we're foreground so it keeps delivering
466
+ try {
467
+ this._foregroundTimer = setInterval(async () => {
468
+ try {
469
+ if (!this.commands) return;
470
+ await this.commands.updateSubscriptions({
471
+ topic: constants_1.Topics.PUBSUB,
472
+ data: { foreground: true }
473
+ });
474
+ this.realtimeDebug('[WATCHDOG] Foreground keepalive sent.');
475
+ } catch (e) {
476
+ this.realtimeDebug('[WATCHDOG] Foreground keepalive failed:', e?.message || e);
477
+ }
478
+ }, KEEPALIVE_FOREGROUND_MS);
479
+ } catch (e) {
480
+ this.realtimeDebug('[WATCHDOG] Could not start foreground timer:', e?.message || e);
481
+ }
482
+
483
+ // Periodically refresh GraphQL/message_sync subscriptions to keep server sending events
484
+ try {
485
+ this._syncTimer = setInterval(async () => {
486
+ try {
487
+ if (!this.commands) return;
488
+ // Refresh the important graphQl subs (idempotent)
489
+ const subs = (this.initOptions && this.initOptions.graphQlSubs && this.initOptions.graphQlSubs.length) ? this.initOptions.graphQlSubs : ['ig_sub_direct', 'ig_sub_direct_v2_message_sync'];
490
+ await this.graphQlSubscribe(subs);
491
+ this.realtimeDebug('[WATCHDOG] MESSAGE_SYNC refreshed.');
492
+ } catch (e) {
493
+ this.realtimeDebug('[WATCHDOG] MESSAGE_SYNC refresh failed:', e?.message || e);
494
+ }
495
+ }, MESSAGE_SYNC_REFRESH_MS);
496
+ } catch (e) {
497
+ this.realtimeDebug('[WATCHDOG] Could not start sync timer:', e?.message || e);
498
+ }
499
+
500
+ // Traffic watchdog: if no messages for a while -> reconnect
501
+ try {
502
+ this._trafficWatchdog = setInterval(async () => {
503
+ try {
504
+ const idle = Date.now() - (this._lastMessageAt || 0);
505
+ if (idle > TRAFFIC_INACTIVITY_MS) {
506
+ this.realtimeDebug(`[WATCHDOG] No traffic detected for ${idle}ms -> attempting reconnect`);
507
+ await this._attemptReconnectSafely();
508
+ }
509
+ } catch (e) {
510
+ this.realtimeDebug('[WATCHDOG] trafficWatchdog fault:', e?.message || e);
511
+ }
512
+ }, Math.min(KEEPALIVE_FOREGROUND_MS, 30000));
513
+ } catch (e) {
514
+ this.realtimeDebug('[WATCHDOG] Could not start traffic watchdog:', e?.message || e);
515
+ }
516
+
517
+ } catch (e) {
518
+ // non-fatal
519
+ this.realtimeDebug('[WATCHDOG] initialization error:', e?.message || e);
520
+ }
521
+ // -------------------- END WATCHDOG --------------------
522
+
448
523
  this._mqtt.on('message', async (msg) => {
449
524
  const topicMap = this.mqtt?.topicMap;
450
525
  const topic = topicMap?.get(msg.topic);
@@ -505,6 +580,34 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
505
580
  }
506
581
  this._setupMessageHandlers();
507
582
  }
583
+
584
+ /**
585
+ * Internal helper to attempt a reconnect safely (debounced)
586
+ */
587
+ async _attemptReconnectSafely() {
588
+ if (this._reconnectInProgress) return;
589
+ this._reconnectInProgress = true;
590
+ try {
591
+ // try graceful disconnect if possible
592
+ try {
593
+ await (this.mqtt?.disconnect?.() ?? Promise.resolve());
594
+ } catch (e) {
595
+ // ignore
596
+ }
597
+ // small delay to ensure socket closed
598
+ await (0, shared_1.delay)(500);
599
+ // reconnect using same initOptions
600
+ try {
601
+ await this.connect(this.initOptions);
602
+ this.realtimeDebug('[WATCHDOG] Reconnect succeeded.');
603
+ } catch (e) {
604
+ this.realtimeDebug('[WATCHDOG] Reconnect failed:', e?.message || e);
605
+ }
606
+ } finally {
607
+ this._reconnectInProgress = false;
608
+ }
609
+ }
610
+
508
611
  /**
509
612
  * Connect from saved session using MultiFileAuthState
510
613
  * @param {Object} authStateHelper - Result from useMultiFileAuthState()
@@ -615,6 +718,25 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
615
718
  }
616
719
  disconnect() {
617
720
  this.safeDisconnect = true;
721
+
722
+ // clear watchdog timers if present
723
+ try {
724
+ if (this._foregroundTimer) {
725
+ clearInterval(this._foregroundTimer);
726
+ this._foregroundTimer = null;
727
+ }
728
+ if (this._syncTimer) {
729
+ clearInterval(this._syncTimer);
730
+ this._syncTimer = null;
731
+ }
732
+ if (this._trafficWatchdog) {
733
+ clearInterval(this._trafficWatchdog);
734
+ this._trafficWatchdog = null;
735
+ }
736
+ } catch (e) {
737
+ // non-fatal
738
+ }
739
+
618
740
  return this.mqtt?.disconnect() ?? Promise.resolve();
619
741
  }
620
742
  graphQlSubscribe(sub) {
@@ -660,4 +782,3 @@ class RealtimeClient extends eventemitter3_1.EventEmitter {
660
782
  }
661
783
  exports.RealtimeClient = RealtimeClient;
662
784
  //# sourceMappingURL=realtime.client.js.map
663
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodejs-insta-private-api-mqtt",
3
- "version": "1.3.21",
3
+ "version": "1.3.22",
4
4
  "description": "Complete Instagram MQTT protocol with FULL iOS + Android support. 33 device presets (21 iOS + 12 Android). iPhone 16/15/14/13/12, iPad Pro, Samsung, Pixel, Huawei. Real-time DM messaging, view-once media extraction, sub-500ms latency.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {