pubsub-plus-emulator

Google cloud pub/sub plus emulator for local testing

Usage no npm install needed!

<script type="module">
  import pubsubPlusEmulator from 'https://cdn.skypack.dev/pubsub-plus-emulator';
</script>

README

Pubsub Plus Emulator

Build Status Coverage Status Maintainability Test Coverage

This is a wrapper for google cloud pub/sub and local testing emulator

Install

npm

$ npm install pubsub-plus-emulator

Using Google Cloud Pubsub locally with Google Cloud Emulator

$ docker run -it -p 8085:8085 knarz/pubsub-emulator

Using Pubsub on staging with google cloud emulator

  • Put the google cloud pubsub service account json file into the root directory of the project
  • rename the service account json file to staging-service-account.json

Using Pubsub on staging with google cloud emulator

  • Put the google cloud pubsub service account json file into the root directory of the project
  • rename the service account json file to production-service-account.json

Usage

The library is imported in either of the following ways:

import PubSub from 'pubsub-plus-emulator';

To publish a message

import PubSub from "pubsub-plus-emulator";

process.env.NODE_ENV = "development";

export const publisher = async (payload) => {
  const pubsubClient = new PubSub();
  await pubsubClient.publish("topicName", payload);
};

publisher(payload)

const payload = {
  firstName: "Ropo",
  lastName: "Olatujoye",
  twitter: "@iamfiropo",
};

To subscribe to an event

import PubSub from 'pubsub-plus-emulator';

const pubsubClient = new PubSub();

pubsubClient.subscribe('topicName', 'subscriptionName', subscriberFunc);

Note: It's important this is an arrow function

const subscriberFunc = (data) => {
    console.log(data);
    // do anything with the received event
}