@riaskov/nevo-messaging 1.1.5 → 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
@@ -446,6 +446,20 @@ createSocketMicroservice({ microserviceName: "user", module: AppModule, port: 80
446
446
  createHttpMicroservice({ microserviceName: "user", module: AppModule, port: 8089 }).then()
447
447
  ```
448
448
 
449
+ NATS reconnect + lazy connect:
450
+
451
+ ```typescript
452
+ createNevoNatsClient(["COORDINATOR"], {
453
+ clientIdPrefix: "user",
454
+ servers: ["nats://127.0.0.1:4222"],
455
+ reconnect: {
456
+ timeWaitMs: 5000,
457
+ maxAttempts: -1,
458
+ lazyConnect: true
459
+ }
460
+ })
461
+ ```
462
+
449
463
  ## Transports
450
464
 
451
465
  | Transport | Patterns | Discovery | Infra | Notes |
@@ -461,7 +475,12 @@ Client factory:
461
475
  ```typescript
462
476
  createNevoNatsClient(["USER", "COORDINATOR"], {
463
477
  clientIdPrefix: "user",
464
- servers: ["nats://127.0.0.1:4222"]
478
+ servers: ["nats://127.0.0.1:4222"],
479
+ reconnect: {
480
+ timeWaitMs: 5000,
481
+ maxAttempts: -1,
482
+ lazyConnect: true
483
+ }
465
484
  })
466
485
  ```
467
486
 
@@ -2,5 +2,14 @@ import { Type } from "@nestjs/common";
2
2
  import { SignalRouterOptions } from "../../signal-router.utils";
3
3
  export interface NatsSignalRouterOptions extends SignalRouterOptions {
4
4
  servers?: string[];
5
+ reconnect?: {
6
+ enabled?: boolean;
7
+ maxAttempts?: number;
8
+ timeWaitMs?: number;
9
+ jitterMs?: number;
10
+ jitterTlsMs?: number;
11
+ waitOnFirstConnect?: boolean;
12
+ lazyConnect?: boolean;
13
+ };
5
14
  }
6
15
  export declare function NatsSignalRouter(serviceType: Type<any> | Type<any>[], options?: NatsSignalRouterOptions): (target: any) => any;
@@ -22,7 +22,21 @@ function NatsSignalRouter(serviceType, options) {
22
22
  target.prototype.onModuleInit = async function () {
23
23
  await originalOnModuleInit.call(this);
24
24
  const servers = options?.servers && options.servers.length > 0 ? options.servers : ["nats://127.0.0.1:4222"];
25
- const nc = await connect({ servers });
25
+ const reconnectEnabled = options?.reconnect?.enabled !== false;
26
+ const maxAttempts = options?.reconnect?.maxAttempts ?? -1;
27
+ const timeWaitMs = options?.reconnect?.timeWaitMs ?? 5000;
28
+ const jitterMs = options?.reconnect?.jitterMs;
29
+ const jitterTlsMs = options?.reconnect?.jitterTlsMs;
30
+ const lazyConnect = options?.reconnect?.lazyConnect === true;
31
+ const waitOnFirstConnect = options?.reconnect?.waitOnFirstConnect ?? !lazyConnect;
32
+ const nc = await connect({
33
+ servers,
34
+ maxReconnectAttempts: reconnectEnabled ? maxAttempts : 0,
35
+ reconnectTimeWait: timeWaitMs,
36
+ reconnectJitter: jitterMs,
37
+ reconnectJitterTLS: jitterTlsMs,
38
+ waitOnFirstConnect
39
+ });
26
40
  this.natsConnection = nc;
27
41
  const sub = nc.subscribe(eventPattern);
28
42
  this.natsSubscription = sub;
@@ -6,6 +6,15 @@ export interface NevoNatsClientOptions {
6
6
  debug?: boolean;
7
7
  serviceName?: string;
8
8
  authToken?: string;
9
+ reconnect?: {
10
+ enabled?: boolean;
11
+ maxAttempts?: number;
12
+ timeWaitMs?: number;
13
+ jitterMs?: number;
14
+ jitterTlsMs?: number;
15
+ waitOnFirstConnect?: boolean;
16
+ lazyConnect?: boolean;
17
+ };
9
18
  backoff?: {
10
19
  enabled?: boolean;
11
20
  baseMs?: number;
@@ -30,8 +30,20 @@ class NevoNatsClient {
30
30
  }
31
31
  static async create(serviceNames, options) {
32
32
  const { connect } = (0, optional_deps_1.getNatsModule)();
33
+ const reconnectEnabled = options?.reconnect?.enabled !== false;
34
+ const maxAttempts = options?.reconnect?.maxAttempts ?? -1;
35
+ const timeWaitMs = options?.reconnect?.timeWaitMs ?? 5000;
36
+ const jitterMs = options?.reconnect?.jitterMs;
37
+ const jitterTlsMs = options?.reconnect?.jitterTlsMs;
38
+ const lazyConnect = options?.reconnect?.lazyConnect === true;
39
+ const waitOnFirstConnect = options?.reconnect?.waitOnFirstConnect ?? !lazyConnect;
33
40
  const nc = await connect({
34
- servers: options?.servers && options.servers.length > 0 ? options.servers : ["nats://127.0.0.1:4222"]
41
+ servers: options?.servers && options.servers.length > 0 ? options.servers : ["nats://127.0.0.1:4222"],
42
+ maxReconnectAttempts: reconnectEnabled ? maxAttempts : 0,
43
+ reconnectTimeWait: timeWaitMs,
44
+ reconnectJitter: jitterMs,
45
+ reconnectJitterTLS: jitterTlsMs,
46
+ waitOnFirstConnect
35
47
  });
36
48
  return new NevoNatsClient(nc, serviceNames, options);
37
49
  }
@@ -205,7 +217,12 @@ class NevoNatsClient {
205
217
  transport: "nats",
206
218
  ts: Date.now()
207
219
  };
208
- this.nc.publish(common_1.DEFAULT_DISCOVERY_TOPIC, this.codec.encode((0, common_1.stringifyWithBigInt)(announcement)));
220
+ try {
221
+ this.nc.publish(common_1.DEFAULT_DISCOVERY_TOPIC, this.codec.encode((0, common_1.stringifyWithBigInt)(announcement)));
222
+ }
223
+ catch (error) {
224
+ console.error("[NevoNatsClient] Discovery publish failed", error);
225
+ }
209
226
  }, this.discoveryHeartbeatIntervalMs);
210
227
  }
211
228
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riaskov/nevo-messaging",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "description": "Microservices messaging framework for NestJS with NATS/Kafka/SocketIO transport",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",