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+ and node.js v12+.
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)]);
Documentation
Functions
- setup(gcpPubSub, projectId)
Sets up the library. This must be called before other functions in this library.
- createTopic(topicName) ⇒
Promise.<Array.<object>>
Creates a Pub/Sub Topic. Idempotent.
- deleteTopic(topicName) ⇒
Promise.<string>
Deletes a Pub/Sub Topic.
- createSubscription(topicName, subscriptionName, [options]) ⇒
Promise.<object>
Create a Pub/Sub subscription. Idempotent.
- deleteSubscription(subscriptionName) ⇒
Promise
Delete a Pub/Sub subscription.
- publish(topicName, message) ⇒
Promise.<object>
publish a message to a topic
- publishJson(topicName, message) ⇒
Promise.<object>
publish a json message to a topic
- pull(subscriptionName, [maxMessages], [returnImmediately]) ⇒
Promise.<Array.<object>>
pull messages from a topic
- acknowledge(subscriptionName, ackIds) ⇒
Promise
acknowledge completion of a pulled message
- subscriptionExists(subscriptionName) ⇒
Promise.<boolean>
Whether or not a subscription exists
- jsonifyMessageData(message) ⇒
*
Takes a JSON pub/sub message and returns the data part as a native javascript value
- topicExists(topicName) ⇒
Promise.<boolean>
Checks whether a topic exists
- getProject() ⇒
string
inspects this module, get what gcp project is being used
- getPublisher() ⇒
PublisherClient
inspects this module, get internal google pub/sub publisher client
- getSubscriber() ⇒
SubscriberClient
inspects this module, get internal google pub/sub subscriber client
- publishMany(topicName, messages) ⇒
Promise.<Array.<PublishResponse>>
Publish many messages to a topic
- publishManyJson(topicName, messages) ⇒
Promise.<Array.<PublishResponse>>
Publish many json messages to a topic
setup(gcpPubSub, projectId)
Sets up the library. This must be called before other functions in this library.
Kind: global function
Param | Type | Description |
---|
gcpPubSub | object | Google Pub/Sub Library from require("@google-cloud/pubsub") |
projectId | string | GCP project id |
createTopic(topicName) ⇒ Promise.<Array.<object>>
Creates a Pub/Sub Topic. Idempotent.
Kind: global function
Returns: Promise.<Array.<object>>
- returns response from Google library's topic.get(): [Topic, apiResponse], see https://googleapis.dev/nodejs/pubsub/latest/global.html#GetTopicCallback
Param | Type | Description |
---|
topicName | string | Name of topic to create |
deleteTopic(topicName) ⇒ Promise.<string>
Deletes a Pub/Sub Topic.
Kind: global function
Returns: Promise.<string>
- returns deleted topicName
Param | Type | Description |
---|
topicName | string | Name of topic to delete |
createSubscription(topicName, subscriptionName, [options]) ⇒ Promise.<object>
Create a Pub/Sub subscription. Idempotent.
Kind: global function
Returns: Promise.<object>
- returns {success: true}
deleteSubscription(subscriptionName) ⇒ Promise
Delete a Pub/Sub subscription.
Kind: global function
Param | Type | Description |
---|
subscriptionName | string | Name of subscription to delete |
publish(topicName, message) ⇒ Promise.<object>
publish a message to a topic
Kind: global function
Returns: Promise.<object>
- Pub/Sub PublishResponse object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse
publishJson(topicName, message) ⇒ Promise.<object>
publish a json message to a topic
Kind: global function
Returns: Promise.<object>
- Pub/Sub PublishResponse object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse
pull(subscriptionName, [maxMessages], [returnImmediately]) ⇒ Promise.<Array.<object>>
pull messages from a topic
Kind: global function
Returns: Promise.<Array.<object>>
- receivedMessages, see: https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PullResponse
Param | Type | Default | Description |
---|
subscriptionName | string | | Name of subscription |
[maxMessages] | number | 1 | Maximum number of messages to pull |
[returnImmediately] | boolean | true | Whether or not to return immediately. If false, waits about 5 seconds then the promise rejects |
acknowledge(subscriptionName, ackIds) ⇒ Promise
acknowledge completion of a pulled message
Kind: global function
Param | Type | Description |
---|
subscriptionName | string | Name of subscription |
ackIds | Array.<string> | The acknowledgment ID for the messages being acknowledged that was returned by the Pub/Sub system in the Pull response. Must not be empty. |
subscriptionExists(subscriptionName) ⇒ Promise.<boolean>
Whether or not a subscription exists
Kind: global function
Param | Type | Description |
---|
subscriptionName | string | Name of subscription |
jsonifyMessageData(message) ⇒ *
Takes a JSON pub/sub message and returns the data part as a native javascript value
Kind: global function
Returns: *
- native javascript value
topicExists(topicName) ⇒ Promise.<boolean>
Checks whether a topic exists
Kind: global function
Param | Type | Description |
---|
topicName | string | name of the topic to check |
getProject() ⇒ string
inspects this module, get what gcp project is being used
Kind: global function
getPublisher() ⇒ PublisherClient
inspects this module, get internal google pub/sub publisher client
Kind: global function
Returns: PublisherClient
- https://googleapis.dev/nodejs/pubsub/latest/v1.PublisherClient.html
getSubscriber() ⇒ SubscriberClient
inspects this module, get internal google pub/sub subscriber client
Kind: global function
Returns: SubscriberClient
- https://googleapis.dev/nodejs/pubsub/latest/v1.SubscriberClient.html
publishMany(topicName, messages) ⇒ Promise.<Array.<PublishResponse>>
Publish many messages to a topic
Kind: global function
Returns: Promise.<Array.<PublishResponse>>
- Pub/Sub PublishResponse objects, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse
publishManyJson(topicName, messages) ⇒ Promise.<Array.<PublishResponse>>
Publish many json messages to a topic
Kind: global function
Returns: Promise.<Array.<PublishResponse>>
- Pub/Sub PublishResponse objects, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PublishResponse
Contributions
Pull Requests are welcome.