Gathering detailed insights and metrics for @alexy4744/nestjs-nats-jetstream-transporter
Gathering detailed insights and metrics for @alexy4744/nestjs-nats-jetstream-transporter
Gathering detailed insights and metrics for @alexy4744/nestjs-nats-jetstream-transporter
Gathering detailed insights and metrics for @alexy4744/nestjs-nats-jetstream-transporter
npm install @alexy4744/nestjs-nats-jetstream-transporter
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (95.6%)
JavaScript (4.25%)
Shell (0.15%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
91,432
Last Day
3
Last Week
37
Last Month
111
Last Year
1,335
16 Stars
61 Commits
10 Forks
3 Watchers
10 Branches
2 Contributors
Updated on Feb 21, 2025
Minified
Minified + Gzipped
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
Cumulative downloads
Total Downloads
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
6
A NATS transporter for NestJS that takes advantage of JetStream for event patterns.
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.
The NatsClient
works mostly the same with the built-in NATS transporter for NestJS. The only difference is that you must instantiate NatsClient
yourself.
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}
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}
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.
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});
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});
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});
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
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
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
42 existing vulnerabilities detected
Details
Score
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