Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
@nestjs-plugins/nestjs-nats-streaming-transport
Advanced tools
Build Event Driven Microservices Architecture with Nats Streaming Server and NestJS.
Exposes the node-nats-streaming library through @ctx context object.
npm i @nestjs/microservices
npm i @nestjs-plugins/nestjs-nats-streaming-transport
docker run -p 4222:4222 -p 8222:8222 nats-streaming -m 8222 -cid 'my-cluster' -SD
Read more about nats-streaming-server
Read more about node-nats-streaming
A simple Event interface used in this example
// @app/events;
export interface UserCreatedEvent {
id: number,
username: string
}
A simple enum to describe pattern used as subjects.
// '@app/events
export enum Patterns {
UserCreated = 'user:created'
}
// app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NatsStreamingTransport } from '@nestjs-plugins/nestjs-nats-streaming-transport'
@Module({
imports: [
NatsStreamingTransport.register(
{
clientId: 'user-service-publisher',
clusterId: 'my-cluster',
connectOptions: {
url: 'http://127.0.0.1:4222',
},
}
),
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
// app.service.ts
import { Injectable } from '@nestjs/common';
import { Publisher } from '@nestjs-plugins/nestjs-nats-streaming-transport';
import { UserCreatedEvent, Patterns } from '@app/events'
@Injectable()
export class AppService {
constructor(private publisher: Publisher) { }
getHello(): string {
const event: UserCreatedEvent = { id: Math.floor(Math.random() * Math.floor(1000)), username: 'bernt' }
this.publisher.emit<string, UserCreatedEvent>(Patterns.UserCreated, event).subscribe(guid => {
console.log('published message with guid:', guid)
})
return `published message: ${JSON.stringify(event)}`
}
}
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Listener } from '@nestjs-plugins/nestjs-nats-streaming-transport'
import { CustomStrategy } from '@nestjs/microservices';
async function bootstrap() {
const options: CustomStrategy = {
strategy: new Listener(
'my-cluster' /* clusterID */,
'user-service-listener' /* clientID */,
'user-service-group', /* queueGroupName */
{
url: 'http://127.0.0.1:4222'
} /* TransportConnectOptions */,
{
durableName: 'user-queue-group',
manualAckMode: true,
deliverAllAvailable: true,
} /* TransportSubscriptionOptions */ ,
),
};
// hybrid microservice and web application
const app = await NestFactory.create(AppModule);
const microService = app.connectMicroservice(options)
microService.listen(() => app.listen(3000))
}
bootstrap();
// app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
import { EventPattern, Payload, Ctx } from '@nestjs/microservices';
import { NatsStreamingContext } from '@nestjs-plugins/nestjs-nats-streaming-transport';
import { Patterns } from '@app/events';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) { }
@Get()
getHello(): string {
return this.appService.getHello();
}
@EventPattern(Patterns.UserCreated)
public async stationCreatedHandler(@Payload() data: { id: number, name: string }, @Ctx() context: NatsStreamingContext) {
console.log(`received message: ${JSON.stringify(data)}`)
context.message.ack()
}
}
FAQs
Nats Streaming Transport for NestJS
We found that @nestjs-plugins/nestjs-nats-streaming-transport demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.