{"id":23634801,"url":"https://github.com/ecobee/nodejs-gcloud-pubsub-module","last_synced_at":"2025-08-31T10:30:36.374Z","repository":{"id":37821329,"uuid":"197390510","full_name":"ecobee/nodejs-gcloud-pubsub-module","owner":"ecobee","description":"A GCloud Pub/Sub module for NestJS","archived":false,"fork":false,"pushed_at":"2024-04-18T16:02:06.000Z","size":202,"stargazers_count":6,"open_issues_count":22,"forks_count":9,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-09T20:46:20.573Z","etag":null,"topics":["gcloud","gcloud-pubsub","home-services-squad","javascript","nestjs","nodejs","pubsub","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ecobee.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-07-17T13:00:54.000Z","updated_at":"2023-04-28T05:44:15.000Z","dependencies_parsed_at":"2023-11-22T16:22:28.866Z","dependency_job_id":"6d3de862-46af-48bf-83c9-c7dc8f81c861","html_url":"https://github.com/ecobee/nodejs-gcloud-pubsub-module","commit_stats":{"total_commits":20,"total_committers":5,"mean_commits":4.0,"dds":"0.30000000000000004","last_synced_commit":"6cadfa3ee3f397eeb96ffddf1b74616a75f174ef"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecobee%2Fnodejs-gcloud-pubsub-module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecobee%2Fnodejs-gcloud-pubsub-module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecobee%2Fnodejs-gcloud-pubsub-module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ecobee%2Fnodejs-gcloud-pubsub-module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ecobee","download_url":"https://codeload.github.com/ecobee/nodejs-gcloud-pubsub-module/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231587571,"owners_count":18396514,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gcloud","gcloud-pubsub","home-services-squad","javascript","nestjs","nodejs","pubsub","typescript"],"created_at":"2024-12-28T05:19:50.835Z","updated_at":"2024-12-28T05:19:51.440Z","avatar_url":"https://github.com/ecobee.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NodeJS GCloud Pub/Sub module\n\n[![CircleCI](https://circleci.com/gh/ecobee/nodejs-gcloud-pubsub-module.svg?style=svg\u0026circle-token=fbf98de8e837e22ab817ad943bd540fd40684955)](https://circleci.com/gh/ecobee/nodejs-gcloud-pubsub-module)\n[![codecov](https://codecov.io/gh/ecobee/nodejs-gcloud-pubsub-module/branch/master/graph/badge.svg?token=F93WezHhBy)](https://codecov.io/gh/ecobee/nodejs-gcloud-pubsub-module)\n\nA Google Cloud Pub/Sub library for the [NestJS](https://github.com/nestjs/nest) framework. Easily subscribe or publish to topics in Google Pub/Sub.\n\n## Features\n\n- Module for sending messages on topics\n- Microservice to subscribe to topics in controllers\n\n## Todo\n\n- Add Module for subscribing to topics\n- Add Microservice client for sending messages on topics\n\n## Installation\n\n**Yarn**\n\n```bash\nyarn add @ecobee/nodejs-gcloud-pubsub-module\n```\n\n**NPM**\n\n```bash\nnpm install @ecobee/nodejs-gcloud-pubsub-module --save\n```\n\n## Usage\n\nPlease refer to the [interfaces](src/interfaces/gcloud-pub-sub.interface.ts) file to understand parameter and object requirements.\n\n### Publish to a topic\n\nPublishing topics follows the classic NestJS pattern of importing a module and injecting a service via dependency injection\n\n#### Module Setup\n\nImport this module into your module\n\n```ts\nimport { Module } from '@nestjs/common';\nimport { GcloudPubSubModule } from 'nodejs-gcloud-pubsub-module'\nimport * as path from 'path';\n\n@Module({\n    imports: [\n        GcloudPubSubModule.forRoot(moduleOptions: GcloudPubSubModuleOptions),\n    ],\n})\nexport class AppModule {}\n```\n\nIf you need to populate the `moduleOptions` with dynamic configuration data use the `forRootAsync` method. The following example pulls configuration objects using the [nestjs-config](https://github.com/nestjsx/nestjs-config) module\n\n```ts\nimport { Module } from '@nestjs/common'\nimport { GcloudPubSubModule } from 'nodejs-gcloud-pubsub-module'\nimport { ConfigService } from 'nestjs-config'\nimport * as path from 'path'\n\n@Module({\n\timports: [\n\t\tGcloudPubSubModule.forRootAsync({\n\t\t\tuseFactory: (config: ConfigService) =\u003e {\n\t\t\t\tconst authOptions = config.get('pubsub.authOptions')\n\t\t\t\tconst publishOptions = config.get('pubsub.publishOptions')\n\t\t\t\treturn {\n\t\t\t\t\tauthOptions /* Authentication options */,\n\t\t\t\t\tpublishOptions /* Message publishing options (Optional) */,\n\t\t\t\t}\n\t\t\t},\n\t\t\tinject: [ConfigService],\n\t\t}),\n\t],\n})\nexport class AppModule {}\n```\n\n#### Service\n\nWith the module imported you can now publish messages to topics\n\n```ts\nimport { Injectable } from '@nestjs/common'\nimport { GcloudPubSubService } from 'nodejs-gcloud-pubsub-module'\n\nconst topic = 'projects/MyProjectId/topics/MyTopic'\n\n@Injectable()\nexport class MyService {\n\tconstructor(private readonly gcloudPubSubService: GcloudPubSubService) {}\n\n\tasync publish(payload: PubSubMessage) {\n\t\tconst serializedPayload = JSON.stringify(payload)\n\t\treturn await this.gcloudPubSubService.publishMessage(topic, serializedPayload)\n\t}\n}\n```\n\n### Subscribing to topics\n\nTo 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.\n\nUsing a Microservice alongside HTTP can be accomplished by creating a Hybrid NestJS application. Refer to [this example](https://github.com/nestjs/nest/blob/master/sample/03-microservices/src/main.ts) to see how this is accomplished.\n\n#### Microservice setup\n\n```ts\nimport { NestFactory } from '@nestjs/core'\nimport { GCloudPubSubServer } from 'nodejs-gcloud-pubsub-module'\nimport { ApplicationModule } from './app.module'\n\nasync function bootstrap() {\n\tconst app = await NestFactory.create(ApplicationModule)\n\n\tconst GCloudPubSubServerOptions = {\n\t\tauthOptions: {/* Authentication options */}),\n\t\tsubscriptionIds: ['subscription-name'],\n\t\tsubscriberOptions: {/* https://googleapis.dev/nodejs/pubsub/latest/global.html#SubscriberOptions */}\n\t}\n\n\tapp.connectMicroservice({\n\t\tstrategy: new GCloudPubSubServer(GCloudPubSubServerOptions),\n\t})\n\n\tawait app.startAllMicroservicesAsync()\n\tawait app.listen(3001)\n}\nbootstrap()\n```\n\n#### Controller\n\n```ts\nimport { Controller } from '@nestjs/common'\nimport { Message } from '@google-cloud/pubsub'\nimport { EventPattern } from '@nestjs/microservices'\n\n@Controller('mycontroller')\nexport class EntitlementController {\n\t@EventPattern('subscription-name')\n\tasync handleCreateEntitlement(message: Message) {\n\t\tconst { data } = message\n\t\tconst messageData = JSON.parse(data.toString())\n\t\t// do stuff with the message\n\t\tmessage.ack()\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecobee%2Fnodejs-gcloud-pubsub-module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fecobee%2Fnodejs-gcloud-pubsub-module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fecobee%2Fnodejs-gcloud-pubsub-module/lists"}