nestjs-pubsub-transport
v1.10.0
Published
Custom transport for Google PubSub for NestJS framework
Downloads
67
Maintainers
Readme
nestjs-pubsub-transport
Custom transport for Google PubSub for NestJS framework
Installation:
npm i nestjs-pubsub-transport
Examples
install module
@Module({
imports: [
PubsubTransportModule.forRootAsync({
inject: [],
useFactory: () => ({
topic: 'topic-name',
subscription: 'subscription-name',
ackDeadline: 10, // optional
maxMessages: 10, // optional
getPattern: (msg: Message) => msg.attributes.yourPattern, // optional
deserializeMessage: (msg: Message) => msg.data,
}),
}),
SomeModule,
],
})
export class AppModule {
}
@Module({
imports: [
PubsubTransportModule.forRoot({
topic: 'topic-name',
subscription: 'subscription-name',
ackDeadline: 10, // optional
maxMessages: 10, // optional
getPattern: (msg: Message) => msg.attributes.yourPattern, // optional
deserializeMessage: (msg: Message) => msg.data,
}),
SomeModule,
],
})
export class AppModule {
}
Controller example
import { Controller } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { PubSubInterceptor } from 'nestjs-pubsub-transport';
// Use interceptor, to correctly catch errors, otherwise transport will always 'ack' message
@UseInterceptors(PubSubInterceptor)
@Controller()
export class ClientsController {
constructor() {
}
// Make sure that your methods in controllers does not return anything, it might break
// error handling logic
@MessagePattern('pattern')
authorize(@Payload() data: AuthorizeClientMessage) {
console.log('handle message', data);
}
}
Connect transport as microservice
// connect pubsub transport
app.connectMicroservice({
strategy: app.get(PubSubTransport),
});
Start transport
await app.startAllMicroservicesAsync();
Bootstrapped with: create-ts-lib-gh
This project is Mit Licensed.