This is a wrapper for google cloud pub/sub and local testing emulator
$ npm install pubsub-plus-emulator
$ docker run -it -p 8085:8085 knarz/pubsub-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
- 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
The library is imported in either of the following ways:
import PubSub from 'pubsub-plus-emulator';
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",
};
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
}