NATS Messaging Client
NATS Messaging Client
Relevant source files
The following files were used as context for generating this wiki page:
The nats module provides a singleton-based management layer for NATS connectivity. It facilitates connection sharing across a microservice, supports JetStream integration, and handles graceful teardown. To maintain a lightweight footprint, the SDK treats nats as an optional peer dependency, allowing services that do not require messaging to omit the library entirely src/nats/nats-client.ts:7-10.
Architecture and Design
The NatsClient class follows a singleton pattern to ensure that only one connection to the NATS server is established and maintained per process src/nats/nats-client.ts:11-13. It provides lazy initialization, meaning the connection is only created when getConnection() is first invoked src/nats/nats-client.ts:15-18.
NATS Client Data Flow
The following diagram illustrates the lifecycle of a NATS connection within the SDK, from initialization to JetStream access and final closure.
NATS Connection Lifecycle
Code
Sources: src/nats/nats-client.ts:15-38
Implementation Details
Connection Management
The client uses the NATS_URL environment variable to locate the server. If this variable is not provided, it defaults to nats://127.0.0.1:4222 src/nats/nats-client.ts:17-18.
| Method | Role | Behavior |
|---|---|---|
getConnection() | Lazy Initializer | Checks if nc exists; if not, calls connect() and initializes JetStream src/nats/nats-client.ts:15-22. |
getJetStream() | Accessor | Returns the initialized JetStreamClient. Throws an error if getConnection() hasn't been called src/nats/nats-client.ts:24-29. |
close() | Teardown | Closes the active connection and nullifies internal references to allow for clean re-initialization src/nats/nats-client.ts:31-38. |
Peer Dependency Model
The SDK is designed so that nats is not a hard requirement. This is achieved by:
- Listing
natsunderdevDependenciesin the SDK itself for testing pnpm-lock.yaml:14-16. - Expecting the consumer to provide the
natspackage if they import thenatsmodule from the SDK src/nats/nats-client.ts:7-9. - Ensuring no other SDK modules (like
configorhttp) import from thenatsdirectory.
Code Entity Mapping
The following diagram maps the logical messaging components to the specific TypeScript entities in the nats module.
Messaging Entity Map
Code
Sources: src/nats/nats-client.ts:1-39
Testing and Singleton Resets
Because NatsClient maintains a static state, tests must explicitly reset the singleton to ensure isolation. The test suite utilizes vi.mock("nats", ...) to intercept connection attempts and NatsClient.close() within beforeEach hooks to clear the internal state between test cases src/nats/tests/nats-client.test.ts:4-20.
Key Test Scenarios:
- Singleton Verification: Ensuring multiple calls to
getConnection()return the exact same instance src/nats/tests/nats-client.test.ts:26-31. - Initialization Guard: Verifying that
getJetStream()throws an error if the connection has not been established yet src/nats/tests/nats-client.test.ts:33-35. - Reconnection Logic: Confirming that calling
close()allows a subsequentgetConnection()to initiate a fresh connection src/nats/tests/nats-client.test.ts:43-49.
Sources: src/nats/tests/nats-client.test.ts:1-54, src/nats/nats-client.ts:1-39