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
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
.
Last updated