Gathering detailed insights and metrics for nestjs-nats-jetstream-transporter
Gathering detailed insights and metrics for nestjs-nats-jetstream-transporter
npm install nestjs-nats-jetstream-transporter
Typescript
Module System
Min. Node Version
Node Version
NPM Version
65.2
Supply Chain
96.6
Quality
74.2
Maintenance
50
Vulnerability
100
License
TypeScript (99.5%)
JavaScript (0.5%)
Verify real, reachable, and deliverable emails with instant MX records, SMTP checks, and disposable email detection.
Total Downloads
1,126
Last Day
1
Last Week
2
Last Month
17
Last Year
196
MIT License
2 Stars
19 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jan 28, 2023
Minified
Minified + Gzipped
Latest Version
1.1.3
Package Id
nestjs-nats-jetstream-transporter@1.1.3
Unpacked Size
31.83 kB
Size
7.87 kB
File Count
19
NPM Version
7.10.0
Node Version
16.0.0
Cumulative downloads
Total Downloads
Last Day
0%
1
Compared to previous day
Last Week
0%
2
Compared to previous week
Last Month
70%
17
Compared to previous month
Last Year
15.3%
196
Compared to previous year
6
A NATS transporter for NestJS that takes advantage of JetStream for event patterns.
1$ npm install nestjs-nats-jetstream-transporter nats@latest
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 options.queue("my-queue-group"); 6 } 7 }) 8});
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) => options.queue("my-queue-group"),
4 queue: "my-queue-group"
5 })
6});
1$ npm run test
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
Reason
Found 0/18 approved changesets -- score normalized to 0
Reason
project is archived
Details
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
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
33 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