3.1.2 • Published 5 months ago

@algoan/nestjs-google-pubsub-microservice v3.1.2

Weekly downloads
185
License
ISC
Repository
github
Last release
5 months ago

NestJS Google Cloud PubSub Micro-service

A custom NestJS Microservice transport strategy using Google Cloud PubSub.

Installation

npm install --save @algoan/pubsub @algoan/nestjs-google-pubsub-microservice

Usage

To start a new Google Cloud PubSub server:

Server setup:

// main.ts
import { GooglePubSubOptions } from '@algoan/pubsub';
import { INestMicroservice } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { GCPubSubServer } from '@algoan/nestjs-google-pubsub-microservice';
import { AppModule } from './app.module';

async function bootstrap() {
  const options: GooglePubSubOptions = {
    projectId: 'test',
    subscriptionsPrefix: 'test-app',
  }

  const app: INestMicroservice = await NestFactory.createMicroservice(AppModule, {
    strategy: new GCPubSubServer(options)
  })

  await app.listen();

  console.log('Server running!')
}
bootstrap()

Controller:

import { EmittedMessage } from '@algoan/pubsub';
import { Controller } from '@nestjs/common';
import { EventPattern, Payload } from '@nestjs/microservices';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  /**
   * Handle the test event
   * @param data Payload sent
   */
  @EventPattern('test_event')
  public async handleTestEvent(@Payload() data: EmittedMessage<{ hello: string }>): Promise<void> {
    /**
     * Handle data emitted by Google PubSub
     */
    this.appService.handleTestEvent(data);
  }
}

EventPattern extras

You can add ListenOptions to the subscription by adding extras to the EventPattern decorator. For example, if you want a different topic name:

/**
  * Handle the test event
  * @param data Payload sent
  */
@EventPattern('test_event', { topicName: 'different_topic' })
public async handleTestEvent(@Payload() data: EmittedMessage<{ hello: string }>): Promise<void> {
  /**
    * Handle data emitted by Google PubSub
    */
  this.appService.handleTestEvent(data);
}

When the application will start and look for event patterns, it will get or create the different_topic topic from Google Cloud.

API

This module uses @algoan/pubsub library which by default automatically acknowledges emitted messages.

GCPubSubServer(options)

Create a new Server instance of Google PubSub. It retrieves all message handlers patterns and creates subscriptions.

  • options: Algoan PubSub options. More information here.
  • options.listenOptions: Global options which will be applied to all subscriptions.
  • options.topicsNames: Only subscribe to topics included in this whitelist.

Other NestJS Google Cloud PubSub server

Other modules implementing Google Cloud PubSub with NestJS microservices:

3.1.2

5 months ago

3.1.1

6 months ago

3.1.0

11 months ago

3.0.9

1 year ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.2

3 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago