nestjs-pubsub-transport

Custom transport for Google PubSub for NestJS framework

Usage no npm install needed!

<script type="module">
  import nestjsPubsubTransport from 'https://cdn.skypack.dev/nestjs-pubsub-transport';
</script>

README

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.