@algoan/pubsub

中文文档
6.4.3发布于 2026/10/22ISCTypeScriptCommonJS构建来源已验证近 30 天下载 1.2万

PubSub Library for algoan

原始使用文档
文档目录15 节
  1. Installation
  2. Usage
  3. Google Cloud PubSub
  4. Contribution
  5. API
  6. PubSubFactory.create({ transport, options })
  7. pubsub.listen(event, opts)
  8. pubsub.emit(event, payload, opts)
  9. pubsub.unsubscribe(event)
  10. Dead Letter Topics
  11. Mode 1: Per-subscription isolated dead-letter topics (recom…
  12. Mode 2: Shared dead-letter topic (single DLT for all subscr…
  13. Mode 3: Per-subscription override
  14. Configuring the drain subscription (e.g. message retention)
  15. No dead-letter topic

PubSub

This is a generic PubSub Factory exposing a listen and a emit method.

NOTE: Today, only Google Cloud PubSub has been added.

Installation

npm install --save @algoan/pubsub

Usage

Google Cloud PubSub

Run tests

To run tests or to try the PubSubFactory class, you need to have a google account and have installed gcloud sdk.

Then, to install the Google PubSub simulator, run:

gcloud components install pubsub-emulator
gcloud components install beta
gcloud components update

Start tests running:

npm test

It will launch a Google PubSub emulator thanks to the google-pubsub-emulator library.

Example

To create a PubSub instance using Google Cloud:

import { EmittedMessage, GCPubSub, PubSubFactory, Transport } from '@algoan/pubsub'

const pubsub: GCPubSub = PubSubFactory.create({
  transport: Transport.GOOGLE_PUBSUB,
  options: {
    projectId: 'test',
    // And all other Google PubSub properties
  }
});
const topicName: string = 'some_topic';

await pubsub.listen(topicName, {
  autoAck: true,
  onMessage: (data: EmittedMessage<{foo: string}>) => {
    console.log(data.parsedData); // { foo: 'bar', time: {Date.now}, _eventName: 'some_topic' }
    // do whatever you want. The message has already been acknowledged
  },
  onError: (error: Error) => {
    // Handle error as you wish
  }
});

await pubsub.emit(topicName, { foo: 'bar' });

Contribution

Thank you for your future contribution 😁 Please follow these instructions before opening a pull request!

API

PubSubFactory.create({ transport, options })

The only static method from the PubSubFactory class. It initiates a new PubSub instance depending on the transport. By default, it connects to Google Cloud PubSub.

  • transport: PubSub technology to use. Only GOOGLE_PUBSUB is available for now.
  • options: Options related to the transport.
    • If transport === Transport.GOOGLE_PUBSUB, then have a look at the Google Cloud PubSub config client.
    • debug: Display logs if it is set to true. It uses a pino logger and pino-pretty if NODE_ENV is not equal to production.
    • pinoOptions: If debug is set to true, set the pino logger options. Default to level: debug and prettyPrint: true if NODE_ENV is not equal to production.
    • topicsPrefix: Add a prefix to all created topics. Example: topicsPrefix: 'my-topic', all topics will begin with my-topic+{your topic name}.
    • topicsSeparator: Customize separator between topicsPrefix and topic name. Example: topicsSeparator: '-', all topics will be {topic prefix}-{your topic name} (default to '+').
    • subscriptionsPrefix: Add a prefix to all created subscriptions. Example: subscriptionsPrefix: 'my-sub', all subscriptions will begin with my-sub%{your topic name}.
    • subscriptionsSeparator: Customize separator between subscriptionsPrefix and topic name. Example: subscriptionsSeparator: '-', all subscriptions will be {subscription prefix}-{your topic name} (default to '%').
    • namespace: Add a namespace property to Message attributes when publishing on a topic.
    • environment: Add a environment property to Message attributes when publishing on a topic.

pubsub.listen(event, opts)

Listen to a specific event.

NOTE: It only uses the Google Cloud subscription pull delivery for now.

  • event: Name of the event.
  • opts: Options related to the Listener method
    • onMessage: Method called when receiving a message
    • onError: Method called when an error occurs
    • options: Option related to the chosen transport

If the chosen transport is Google Cloud PubSub, then options would be:

  • autoAck: Automatically ACK an event as soon as it is received (default to true)
  • subscriptionOptions: Options related to the created Subscription:
    • name: Custom name for the subscription. Default: event (also equal to the topic name)
    • get: Options applied to the getSubscription method (have a look at Subscription options)
    • sub: Options applied to the subscription instance (see also setOptions method)
    • create: Options applied to the createSubscription method (have a look at Create Subscription options)
    • deadLetterTopicName: Per-subscription override for the dead-letter topic name (see Dead Letter Topics)
  • topicOptions: Options applied to the created topic (have a look at Topic options)
  • topicName: Set the topic name. By default, it uses the default name with a prefix.

pubsub.emit(event, payload, opts)

Emit a specific event with a payload. It added attributes in the message if you have added a namespace or an environment when setting the PubSubFactory class. It also adds an _eventName and a time property in the emitted payload.

  • event: Name of the event to emit.
  • payload: Payload to send. It will be buffered by Google, and then parsed by the listen method.
  • opts: Options related to the Emit method
    • metadata: Custom metadata added to the message
    • options: Option related to the chosen transport

If the chosen transport is Google Cloud PubSub, then options would be:

  • topicOptions: Options applied to the created topic (have a look at Topic options)
  • publishOptions: Publish options set to the topic after its creation. Refer to Publish Options
  • messageOptions: Additional message options added to the message. Refer to Message Options

pubsub.unsubscribe(event)

Stop the server connection for a given subscription.

  • event: Name of of the event to stop listening for.

Dead Letter Topics

When deadLetterOptions is set in the constructor options, the library automatically:

  1. Creates the dead-letter topic (if it does not exist)
  2. Creates a drain subscription on the dead-letter topic (to prevent GCP from discarding undelivered messages)
  3. Grants roles/pubsub.publisher on the dead-letter topic to the GCP Pub/Sub service account
  4. Grants roles/pubsub.subscriber on the source subscription to the GCP Pub/Sub service account
  5. Applies the deadLetterPolicy to the created subscription

This setup only happens once per new subscription — reconnecting to an already-existing subscription incurs zero overhead.

Omit deadLetterTopicName. Each subscription automatically gets its own <subscriptionName>-deadletter topic, so failed messages from different consumers are isolated and can be replayed independently.

const pubsub: GCPubSub = PubSubFactory.create({
  transport: Transport.GOOGLE_PUBSUB,
  options: {
    projectId: 'my-project',
    deadLetterOptions: {
      maxDeliveryAttempts: 10, // optional, defaults to 5
    },
  },
});

// Each listen() call creates its own isolated dead-letter topic:
// - "my-orders-sub-deadletter" for the first subscription
// - "my-payments-sub-deadletter" for the second
await pubsub.listen('my-orders-sub');
await pubsub.listen('my-payments-sub');

Mode 2: Shared dead-letter topic (single DLT for all subscriptions)

Provide deadLetterTopicName to route all failed messages to one shared topic. The topic must already exist in GCP.

const pubsub: GCPubSub = PubSubFactory.create({
  transport: Transport.GOOGLE_PUBSUB,
  options: {
    projectId: 'my-project',
    deadLetterOptions: {
      deadLetterTopicName: 'projects/my-project/topics/my-dead-letter', // fully-qualified or short name
      maxDeliveryAttempts: 5,
    },
  },
});

await pubsub.listen('my-orders-sub');   // routes to "my-dead-letter"
await pubsub.listen('my-payments-sub'); // routes to "my-dead-letter"

Mode 3: Per-subscription override

Override the dead-letter topic for a specific subscription via subscriptionOptions.deadLetterTopicName. Falls back to the instance-level deadLetterTopicName, then auto-derives if neither is set.

await pubsub.listen('my-special-sub', {
  options: {
    subscriptionOptions: {
      deadLetterTopicName: 'projects/my-project/topics/special-dead-letter',
    },
  },
});

Configuring the drain subscription (e.g. message retention)

By default the drain subscription created on the dead-letter topic uses Google Cloud defaults — notably a 7-day message retention. Provide drainSubscriptionOptions to override the CreateSubscriptionOptions used when the drain subscription is created (for example, to keep dead-lettered messages for the maximum of 31 days so there is more time to inspect and replay them).

const pubsub: GCPubSub = PubSubFactory.create({
  transport: Transport.GOOGLE_PUBSUB,
  options: {
    projectId: 'my-project',
    deadLetterOptions: {
      drainSubscriptionOptions: {
        messageRetentionDuration: { seconds: 2678400 }, // 31 days (max)
      },
    },
  },
});

await pubsub.listen('my-orders-sub');

Like the rest of the dead-letter setup, this is only applied when the drain subscription is created — an already-existing drain subscription is left untouched.

No dead-letter topic

If deadLetterOptions is not set, no dead-letter resources are created and no IAM calls are made — fully backward compatible.

const pubsub: GCPubSub = PubSubFactory.create({
  transport: Transport.GOOGLE_PUBSUB,
  options: {
    projectId: 'my-project',
    // no deadLetterOptions — existing behavior unchanged
  },
});

版本与发布记录

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

版本发布日期距今
6.4.3latest2026/10/2217 天前
6.4.22026/09/221 个月前
6.4.02026/08/172 个月前
6.3.12026/06/164 个月前
6.3.02026/03/197 个月前
6.2.02024/02/262 年前

关于 @algoan/pubsub 的常见问题

@algoan/pubsub 是什么?

PubSub Library for algoan

如何安装 @algoan/pubsub?

在项目目录运行 npm install @algoan/pubsub,或使用 pnpm add / yarn add / bun add。

@algoan/pubsub 支持 TypeScript 吗?

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

@algoan/pubsub 支持 ESM 还是 CommonJS?

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

@algoan/pubsub 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^22.13.0 || >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@algoan/pubsub 可以商用吗?

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

@algoan/pubsub 的下载量有多少?

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

@algoan/pubsub 还在维护吗?

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