@patricktobias86/node-red-telegram-account 1.1.4 → 1.1.6

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/README.md CHANGED
@@ -14,3 +14,9 @@ npm i @patricktobias86/node-red-telegram-account
14
14
 
15
15
  - resolve-userid – resolve a Telegram username to its numeric user ID
16
16
 
17
+ ## Session management
18
+
19
+ Connections to Telegram are cached by the configuration node. When the flow is
20
+ redeployed, the existing session is reused instead of creating a new one.
21
+ The client is only disconnected once no nodes reference that session anymore.
22
+
package/nodes/config.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const { TelegramClient } = require("telegram");
2
2
  const { StringSession } = require("telegram/sessions");
3
3
 
4
- const activeClients = {}; // Cache: session string → connected client
4
+ const activeClients = {}; // Cache: session string → { client, refCount }
5
5
 
6
6
  module.exports = function (RED) {
7
7
  function TelegramClientConfig(config) {
@@ -18,7 +18,9 @@ module.exports = function (RED) {
18
18
 
19
19
  if (activeClients[sessionStr]) {
20
20
  // Reuse existing client
21
- this.client = activeClients[sessionStr];
21
+ const record = activeClients[sessionStr];
22
+ this.client = record.client;
23
+ record.refCount += 1;
22
24
  node.status({ fill: "green", shape: "dot", text: "Reused existing client" });
23
25
  } else {
24
26
  // Create and connect new client
@@ -28,24 +30,35 @@ module.exports = function (RED) {
28
30
  requestRetries: config.requestRetries || 5,
29
31
  });
30
32
 
33
+ // Pre-store with refCount to ensure reuse during connection setup
34
+ activeClients[sessionStr] = { client: this.client, refCount: 1 };
35
+
31
36
  this.client.connect().then(async () => {
32
37
  const authorized = await this.client.isUserAuthorized();
33
38
  if (!authorized) {
34
39
  node.error("Session is invalid");
35
40
  } else {
36
41
  node.status({ fill: "green", shape: "dot", text: "Connected" });
37
- activeClients[sessionStr] = this.client;
38
42
  }
39
43
  }).catch(err => {
40
44
  node.error("Connection error: " + err.message);
45
+ delete activeClients[sessionStr];
41
46
  });
42
47
  }
43
48
 
44
49
  this.on("close", async () => {
45
- if (this.client && activeClients[sessionStr] === this.client) {
46
- await this.client.disconnect();
47
- delete activeClients[sessionStr];
48
- node.status({ fill: "red", shape: "ring", text: "Disconnected" });
50
+ const record = activeClients[sessionStr];
51
+ if (record && record.client === this.client) {
52
+ record.refCount -= 1;
53
+ if (record.refCount <= 0) {
54
+ try {
55
+ await this.client.disconnect();
56
+ } catch (err) {
57
+ node.error("Disconnect error: " + err.message);
58
+ }
59
+ delete activeClients[sessionStr];
60
+ node.status({ fill: "red", shape: "ring", text: "Disconnected" });
61
+ }
49
62
  }
50
63
  });
51
64
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patricktobias86/node-red-telegram-account",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Node-RED nodes to communicate with GramJS.",
5
5
  "main": "nodes/config.js",
6
6
  "keywords": [