Installations
npm install @alexy4744/nestjs-nats-jetstream-transporter
Developer Guide
Typescript
No
Module System
CommonJS
Min. Node Version
>=12.0.0
Node Version
16.0.0
NPM Version
7.10.0
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (95.6%)
JavaScript (4.25%)
Shell (0.15%)
validate.email 🚀
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Developer
alexy4744
Download Statistics
Total Downloads
91,432
Last Day
3
Last Week
37
Last Month
111
Last Year
1,335
GitHub Statistics
16 Stars
61 Commits
10 Forks
3 Watchers
10 Branches
2 Contributors
Updated on Feb 21, 2025
Bundle Size
6.96 kB
Minified
1.88 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.0.2
Package Id
@alexy4744/nestjs-nats-jetstream-transporter@1.0.2
Unpacked Size
39.71 kB
Size
9.44 kB
File Count
27
NPM Version
7.10.0
Node Version
16.0.0
Total Downloads
Cumulative downloads
Total Downloads
91,432
Last Day
-25%
3
Compared to previous day
Last Week
60.9%
37
Compared to previous week
Last Month
24.7%
111
Compared to previous month
Last Year
-87.2%
1,335
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
6
NestJS NATS JetStream Transporter
A NATS transporter for NestJS that takes advantage of JetStream for event patterns.
Installation
1$ npm install @alexy4744/nestjs-nats-jetstream-transporter nats@2.2.0
⚠️ This library uses NATS.js, which means that the underlying JetStream API is subject to change and does not follow semver. It is recommended to use nats@2.2.0
until the JetStream API is out of beta as it has been fully tested with this package.
Publishing Messages
The NatsClient
works mostly the same with the built-in NATS transporter for NestJS. The only difference is that you must instantiate NatsClient
yourself.
Request-Reply
1// main.ts 2import { NatsTransportStrategy } from "nestjs-nats-jetstream-transporter"; 3 4const app = await NestFactory.createMicroservice(AppModule, { 5 strategy: new NatsTransportStrategy() 6}); 7 8await app.listenAsync();
1// math.controller.ts 2import { NatsClient } from "nestjs-nats-jetstream-transporter"; 3 4@Controller() 5export class MathController { 6 private readonly client = new NatsClient(); 7 8 accumulate(): Observable<number> { 9 return this.client.send<number>("sum", [1, 2, 3]); 10 } 11}
Event-Based
1// main.ts
2import { NatsTransportStrategy } from "nestjs-nats-jetstream-transporter";
3
4const app = await NestFactory.createMicroservice(AppModule, {
5 strategy: new NatsTransportStrategy({
6 // Create a stream with a subject called "orders.created"
7 // This is important later on when we publish an event with NatsClient
8 streams: [
9 {
10 name: "orders-events",
11 subjects: ["orders.created"]
12 }
13 ]
14 })
15});
16
17await app.listenAsync();
1// orders.controller.ts 2import { NatsClient } from "nestjs-nats-jetstream-transporter"; 3 4@Controller() 5export class OrdersController { 6 private readonly client = new NatsClient(); 7 8 constructor(private readonly ordersService: OrdersService) {} 9 10 async create(): Promise<void> { 11 const order = await this.ordersService.create(); 12 13 this.client.emit("orders.created", order); 14 } 15}
Receiving Messages
There are no special changes needed to receive messages. Just use the @EventPattern()
and @MessagePattern()
decorators provided by NestJS.
@Ctx()
works exactly the same, however you should use the NatsContext
provided by this package as the parameter type. It exposes an additional getMessage()
method that returns the original message object if needed.
Customizing JetStream consumer options
You can customize how the JetStream push consumer behaves. One example is making the consumer durable to survive application restarts. Another example is taking advantage of distributed queues for horozontal scaling.
Read more about JetStream consumers here, and refer to the underlying API here.
1const app = await NestFactory.createMicroservice(AppModule, { 2 strategy: new NatsTransportStrategy({ 3 consumer: (options) => { 4 options.durable("my-durable-name"); 5 6 // When using a queue group with JetStream, it is necessary that deliverTo() and queue() uses the same name. 7 // This is a requirement for NATS.js v2.2.0, see this issue for more details: 8 // https://github.com/nats-io/nats.js/issues/446 9 options.deliverTo("my-queue-group"); 10 options.queue("my-queue-group"); 11 } 12 }) 13});
NACK and TERM JetStream messages
By default, all JetStream messages are automatically acknowledged. However, you can also NACK and TERM the message by returning the respective symbols from your application.
Returning NACK
will ask for the message to be redelivered, while TERM
will terminate future deliveries of the message.
1// shipping.controller.ts 2import { NACK, TERM } from "nestjs-nats-jetstream-transporter"; 3 4@Controller() 5export class ShippingController { 6 constructor(private readonly shippingService: ShippingService) {} 7 8 @EventPattern("orders.created") 9 handleCreatedOrder(order) { 10 // If a shipment cannot be scheduled at this time, then ask for the message to be redelivered 11 if (this.shippingService.isBusy()) { 12 return NACK; 13 } 14 15 // If a shipment already exists for this order, then terminate the redelivery of this message 16 if (this.shippingService.exists(order)) { 17 return TERM; 18 } 19 20 this.shippingService.scheduleShipment(order); 21 22 // Otherwise, the message will be auto-acked 23 } 24}
If the handler for your event pattern throws an error, the message will automatically be terminated. You can change this behavior by providing an onError
function to the transport strategy options.
1const app = await NestFactory.createMicroservice(AppModule, {
2 strategy: new NatsTransportStrategy({
3 // Messages that caused an exception will be acked instead
4 onError: (message) => message.ack()
5 })
6});
Queue Groups
You can specify a queue group name to enable distributed queues. This will load balance message delivery across all other application instances with the same queue group name. It makes horozontal scaling simple as you can scale up by running another instance of your application with no additional configuration.
1const app = await NestFactory.createMicroservice(AppModule, {
2 strategy: new NatsTransportStrategy({
3 queue: "my-queue-group"
4 })
5});
If you want to enable this functionality for event patterns, you must also specify the queue group name using the JetStream consumer options builder.
1const app = await NestFactory.createMicroservice(AppModule, { 2 strategy: new NatsTransportStrategy({ 3 consumer: (options) => { 4 // When using a queue group with JetStream, it is necessary that deliverTo() and queue() uses the same name. 5 // This is a requirement for NATS.js v2.2.0, see this issue for more details: 6 // https://github.com/nats-io/nats.js/issues/446 7 options.deliverTo("my-queue-group"); 8 options.queue("my-queue-group") 9 }, 10 queue: "my-queue-group" 11 }) 12});
Development
1# Run tests 2$ nx test nestjs-nats-jetstream-transporter
1# Update version 2$ nx version nestjs-nats-jetstream-transporter
1# Build the project 2$ nx build nestjs-nats-jetstream-transporter
1# Publish new version on GitHub 2$ git push --follow-tags origin master
1# Publish new version on NPM 2$ npm publish ./dist/packages/nestjs-nats-jetstream-transporter --access=public

No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:31: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:56: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:62: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:67: update your workflow using https://app.stepsecurity.io/secureworkflow/alexy4744/packages/test.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 2 out of 2 npmCommand dependencies pinned
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/test.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 12 are checked with a SAST tool
Reason
42 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-4jpv-8r57-pv7j
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-cph5-m8f7-6c5x
- Warn: Project is vulnerable to: GHSA-wf5p-g6vw-rhxx
- Warn: Project is vulnerable to: GHSA-jr5f-v2jv-69x6
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-h452-7996-h45h
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-wm7h-9275-46v2
- Warn: Project is vulnerable to: GHSA-ghr5-ch3p-vcr6
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-vjh7-7g9h-fjfh
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.4
/10
Last Scanned on 2025-03-10
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More