> For the complete documentation index, see [llms.txt](https://docs.nestjstools.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nestjstools.com/broker-integration/google-pubsub.md).

# 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:

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

or

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

***

#### ⚙️ Example Configuration

```ts
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`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nestjstools.com/broker-integration/google-pubsub.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
