A Google Cloud Pub/Sub library for the NestJS framework. Easily subscribe or publish to topics in Google Pub/Sub.
- Module for sending messages on topics
- Microservice to subscribe to topics in controllers
- Add Module for subscribing to topics
- Add Microservice client for sending messages on topics
Yarn
yarn add @ecobee/nodejs-gcloud-pubsub-moduleNPM
npm install @ecobee/nodejs-gcloud-pubsub-module --savePlease refer to the interfaces file to understand parameter and object requirements.
Publishing topics follows the classic NestJS pattern of importing a module and injecting a service via dependency injection
Import this module into your module
import{Module}from'@nestjs/common';import{GcloudPubSubModule}from'nodejs-gcloud-pubsub-module'import*aspathfrom'path';
@Module({imports: [GcloudPubSubModule.forRoot(moduleOptions: GcloudPubSubModuleOptions),],})exportclassAppModule{}If you need to populate the moduleOptions with dynamic configuration data use the forRootAsync method. The following example pulls configuration objects using the nestjs-config module
import{Module}from'@nestjs/common'import{GcloudPubSubModule}from'nodejs-gcloud-pubsub-module'import{ConfigService}from'nestjs-config'import*aspathfrom'path'
@Module({imports: [GcloudPubSubModule.forRootAsync({useFactory: (config: ConfigService)=>{constauthOptions=config.get('pubsub.authOptions')constpublishOptions=config.get('pubsub.publishOptions')return{
authOptions /* Authentication options */,
publishOptions /* Message publishing options (Optional) */,}},inject: [ConfigService],}),],})exportclassAppModule{}With the module imported you can now publish messages to topics
import{Injectable}from'@nestjs/common'import{GcloudPubSubService}from'nodejs-gcloud-pubsub-module'consttopic='projects/MyProjectId/topics/MyTopic'
@Injectable()exportclassMyService{constructor(privatereadonlygcloudPubSubService: GcloudPubSubService){}asyncpublish(payload: PubSubMessage){constserializedPayload=JSON.stringify(payload)returnawaitthis.gcloudPubSubService.publishMessage(topic,serializedPayload)}}To subscribe to pubsub message topics we leverage a custom NestJS Microservice. This has the benefit of allowing us to decorate our controllers as we would for standard HTTP requests.
Using a Microservice alongside HTTP can be accomplished by creating a Hybrid NestJS application. Refer to this example to see how this is accomplished.
import{NestFactory}from'@nestjs/core'import{GCloudPubSubServer}from'nodejs-gcloud-pubsub-module'import{ApplicationModule}from'./app.module'asyncfunctionbootstrap(){constapp=awaitNestFactory.create(ApplicationModule)constGCloudPubSubServerOptions={authOptions: {/* Authentication options */}),subscriptionIds: ['subscription-name'],subscriberOptions: {/* https://googleapis.dev/nodejs/pubsub/latest/global.html#SubscriberOptions */}}app.connectMicroservice({strategy: newGCloudPubSubServer(GCloudPubSubServerOptions),})awaitapp.startAllMicroservicesAsync()awaitapp.listen(3001)}bootstrap()import{Controller}from'@nestjs/common'import{Message}from'@google-cloud/pubsub'import{EventPattern}from'@nestjs/microservices'
@Controller('mycontroller')exportclassEntitlementController{
@EventPattern('subscription-name')asynchandleCreateEntitlement(message: Message){const{ data }=messageconstmessageData=JSON.parse(data.toString())// do stuff with the messagemessage.ack()}}