gcp-pubsub-lite
This is a convenience library/wrapper for the official GCP Pub/Sub library. You supply our wrapper with the official GCP Pub/Sub library so you control which version you want to use. This way, our library will not block you from applying e.g. the latest security updates, or pinning to a previous version. We will keep this library up-to-date to be compatible with recent versions of the official library. Currently we support @google-cloud/pubsub
versions 1.4.1+.
The official Google library, while full-featured, requires focused reading to understand and boilerplate to accomplish simple tasks. It uses an OO approach where the same things can be accomplished with different classes, in slightly different ways. By contrast, gcp-pubsub-lite
uses simple, easy to use, functions. For example, gcp-pubsub-lite
enables simple subscription polling and sending/receiving JSON data as shown below.
Installation
npm i @google-cloud/pubsub @pluralsight/gcp-pubsub-lite
Usage Example
const gcpPubSub = require("@google-cloud/pubsub");
const pubsub = require("@pluralsight/gcp-pubsub-lite");
const {GCP_PROJECT_ID: gcpProjectId} = process.env;
pubsub.setup(gcpPubSub, gcpProjectId);
const topicName = "topicName";
await pubsub.createTopic(topicName);
const subName = "subName";
await pubsub.createSubscription(topicName, subName);
const messageData = {test: true, count: 5, data: "foobar", pi: 3.14};
await pubsub.publishJson(topicName, messageData);
let isPolling = true
while (isPolling) {
let envelopes = []
envelopes = await pubsub.pull(subName, 1);
if (!envelopes.length) {
await new Promise(resolve => setTimeout(resolve, 500));
continue;
}
const [envelope] = envelopes;
const {message, ackId} = envelope;
const copyOfMessageData = pubsub.jsonifyMessageData(message);
console.log("message", copyOfMessageData);
await pubsub.acknowledge(subName, [ackId]);
}
await Promise.all([pubsub.deleteSubscription(subName),
pubsub.deleteTopic(topicName)]);
Contributions
Pull Requests are welcome.