npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ecobee/nodejs-gcloud-pubsub-module

v0.8.0

Published

A GCloud Pub/Sub module for NestJS

Downloads

387

Readme

NodeJS GCloud Pub/Sub module

CircleCI codecov

A Google Cloud Pub/Sub library for the NestJS framework. Easily subscribe or publish to topics in Google Pub/Sub.

Features

  • Module for sending messages on topics
  • Microservice to subscribe to topics in controllers

Todo

  • Add Module for subscribing to topics
  • Add Microservice client for sending messages on topics

Installation

Yarn

yarn add @ecobee/nodejs-gcloud-pubsub-module

NPM

npm install @ecobee/nodejs-gcloud-pubsub-module --save

Usage

Please refer to the interfaces file to understand parameter and object requirements.

Publish to a topic

Publishing topics follows the classic NestJS pattern of importing a module and injecting a service via dependency injection

Module Setup

Import this module into your module

import { Module } from '@nestjs/common';
import { GcloudPubSubModule } from 'nodejs-gcloud-pubsub-module'
import * as path from 'path';

@Module({
    imports: [
        GcloudPubSubModule.forRoot(moduleOptions: GcloudPubSubModuleOptions),
    ],
})
export class AppModule {}

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 * as path from 'path'

@Module({
	imports: [
		GcloudPubSubModule.forRootAsync({
			useFactory: (config: ConfigService) => {
				const authOptions = config.get('pubsub.authOptions')
				const publishOptions = config.get('pubsub.publishOptions')
				return {
					authOptions /* Authentication options */,
					publishOptions /* Message publishing options (Optional) */,
				}
			},
			inject: [ConfigService],
		}),
	],
})
export class AppModule {}

Service

With the module imported you can now publish messages to topics

import { Injectable } from '@nestjs/common'
import { GcloudPubSubService } from 'nodejs-gcloud-pubsub-module'

const topic = 'projects/MyProjectId/topics/MyTopic'

@Injectable()
export class MyService {
	constructor(private readonly gcloudPubSubService: GcloudPubSubService) {}

	async publish(payload: PubSubMessage) {
		const serializedPayload = JSON.stringify(payload)
		return await this.gcloudPubSubService.publishMessage(topic, serializedPayload)
	}
}

Subscribing to topics

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.

Microservice setup

import { NestFactory } from '@nestjs/core'
import { GCloudPubSubServer } from 'nodejs-gcloud-pubsub-module'
import { ApplicationModule } from './app.module'

async function bootstrap() {
	const app = await NestFactory.create(ApplicationModule)

	const GCloudPubSubServerOptions = {
		authOptions: {/* Authentication options */}),
		subscriptionIds: ['subscription-name'],
		subscriberOptions: {/* https://googleapis.dev/nodejs/pubsub/latest/global.html#SubscriberOptions */}
	}

	app.connectMicroservice({
		strategy: new GCloudPubSubServer(GCloudPubSubServerOptions),
	})

	await app.startAllMicroservicesAsync()
	await app.listen(3001)
}
bootstrap()

Controller

import { Controller } from '@nestjs/common'
import { Message } from '@google-cloud/pubsub'
import { EventPattern } from '@nestjs/microservices'

@Controller('mycontroller')
export class EntitlementController {
	@EventPattern('subscription-name')
	async handleCreateEntitlement(message: Message) {
		const { data } = message
		const messageData = JSON.parse(data.toString())
		// do stuff with the message
		message.ack()
	}
}