nestjs-pubsub-transport
TypeScript icon, indicating that this package has built-in type declarations

1.10.0 • Public • Published

nestjs-pubsub-transport

Deploy Coverage Status

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.

Package Sidebar

Install

npm i nestjs-pubsub-transport

Weekly Downloads

0

Version

1.10.0

License

MIT

Unpacked Size

27.6 kB

Total Files

15

Last publish

Collaborators

  • tricky-max