@algoan/nestjs-google-pubsub-microservice

中文文档
3.2.0发布于 2026/09/24ISCTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 1.1万

A GooglePubSub transport strategy with NestJS Microservice

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. EventPattern extras
  4. API
  5. GCPubSubServer(options)
  6. Other NestJS Google Cloud PubSub server

Nest Logo

A Nest Google PubSub Microservice.

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:

版本与发布记录

@algoan/nestjs-google-pubsub-microservice 共发布 30 个版本,首次发布于 2020/04/22;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.2.0latest2026/09/241 个月前
3.1.42025/06/111.4 年前
3.1.32024/09/162 年前
3.1.22023/12/112 年前
3.1.12023/11/102 年前
3.1.02023/06/173 年前

关于 @algoan/nestjs-google-pubsub-microservice 的常见问题

@algoan/nestjs-google-pubsub-microservice 是什么?

A GooglePubSub transport strategy with NestJS Microservice

如何安装 @algoan/nestjs-google-pubsub-microservice?

在项目目录运行 npm install @algoan/nestjs-google-pubsub-microservice,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@nestjs/core(>=8)、@algoan/pubsub(>=4 < 7)、@nestjs/common(>=8)、@nestjs/microservices(>=8)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@algoan/nestjs-google-pubsub-microservice 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@algoan/nestjs-google-pubsub-microservice 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@algoan/nestjs-google-pubsub-microservice 可以商用吗?

@algoan/nestjs-google-pubsub-microservice 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@algoan/nestjs-google-pubsub-microservice 的下载量有多少?

根据 npm 官方下载统计,@algoan/nestjs-google-pubsub-microservice 在 2026/10/06 – 2026/11/04 这 30 天内被下载 10,726 次,日均约 358 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@algoan/nestjs-google-pubsub-microservice 还在维护吗?

最新版本 3.2.0 发布于 2026/09/24(1 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 1 个;首次发布于 2020/04/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。