Nestjstools Messaging Docs
  • Introduction
    • What is this library?
    • Supported message brokers
  • Getting Started
    • Installation
    • Initialize Module
    • Message Handler
    • Disaptch a message
  • Components
    • Message Handlers
    • Normalizers
    • Exception Listeners
    • Middlewares
    • Message Bus
    • Channel
  • Broker integration
    • RabbitMQ
    • Redis
    • Google PubSub
    • Amazon SQS
    • Nats
  • Best practice
    • CQRS based on RabbitMQ
    • Create wrapper class for Message Bus
    • Consumer as the background process
Powered by GitBook
On this page
Export as PDF
  1. Broker integration

Google PubSub

Google Pub/Sub Channel Integration

The Google Pub/Sub extension for @nestjstools/messaging enables cloud-native, event-driven communication across distributed systems. It’s ideal for scalable, serverless-friendly applications that rely on Google Cloud infrastructure.


πŸ“¦ Installation

Install the core messaging package and the Google Pub/Sub extension:

npm install @nestjstools/messaging @nestjstools/messaging-google-pubsub-extension

or

yarn add @nestjstools/messaging @nestjstools/messaging-google-pubsub-extension

βš™οΈ Example Configuration

import { MessagingModule } from '@nestjstools/messaging';
import { PubSubChannelConfig, MessagingGooglePubSubExtensionModule } from '@nestjstools/messaging-google-pubsub-extension';
import { InMemoryChannelConfig } from '@nestjstools/messaging/channels';
import { SendMessageHandler } from './handlers/send-message.handler';

@Module({
  imports: [
    MessagingGooglePubSubExtensionModule,
    MessagingModule.forRoot({
      messageHandlers: [SendMessageHandler],
      buses: [
        {
          name: 'default.bus',
          channels: ['in-memory', 'pubsub-events'],
        },
      ],
      channels: [
        new InMemoryChannelConfig({
          name: 'in-memory',
          avoidErrorsForNotExistedHandlers: true,
        }),
        new PubSubChannelConfig({
          name: 'pubsub-events',
          projectId: 'your-gcp-project-id',
          topic: 'your-topic-name',
          subscription: 'your-subscription-name',
          middlewares: [],
          autoCreate: true,
          enableSubscriber: true,
          avoidErrorsForNotExistedHandlers: true,
        }),
      ],
      debug: true,
    }),
  ],
})
export class AppModule {}

πŸ› οΈ PubSubChannelConfig Properties

Property
Description
Default Value

name

The channel name (e.g., 'pubsub-events').

β€”

projectId

Google Cloud project ID.

β€”

topic

The Pub/Sub topic to publish messages to.

β€”

subscription

Subscription name to consume messages from.

β€”

middlewares

Array of middlewares applied to incoming/outgoing messages.

[]

autoCreate

Automatically create topic/subscription if they do not exist.

true

enableSubscriber

Enables the background subscriber to listen to incoming Pub/Sub messages.

true

avoidErrorsForNotExistedHandlers

Silently skips messages without a matching handler.

false


🌐 Key Features

  • Cloud-Native Messaging: Leverages Google Pub/Sub for reliable, asynchronous communication across microservices.

  • Auto Resource Provisioning: Automatically creates missing topics and subscriptions when autoCreate is enabled.

  • Efficient Scaling: Suitable for horizontally scaled environments (e.g., Kubernetes, Cloud Run).

  • Graceful Error Handling: Skip errors when no handler exists by enabling avoidErrorsForNotExistedHandlers.

  • Subscriber Control: Toggle subscriber activation with enableSubscriber.

PreviousRedisNextAmazon SQS

Last updated 3 days ago