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
npm i @google-cloud/pubsub @pluralsight/gcp-pubsub-lite
# yarn
yarn add @google-cloud/pubsub @pluralsight/gcp-pubsub-lite
Usage Example
const gcpPubSub = require("@google-cloud/pubsub");
const psLite = require("@pluralsight/gcp-pubsub-lite");
const {GCP_PROJECT_ID: gcpProjectId} = process.env;
psLite.setup(gcpPubSub, gcpProjectId);
const topicName = "topicName";
await psLite.createTopic(topicName); // idempotent
const subName = "subName";
await psLite.createSubscription(topicName, subName); // idempotent
const messageData = {test: true, count: 5, data: "foobar", pi: 3.14};
await psLite.publishJson(topicName, messageData);
let isPolling = true;
while (isPolling) {
const envelopes = await psLite.pull(subName, 1);
if (!envelopes.length) {
// wait 500ms and try again
await new Promise(resolve => setTimeout(resolve, 500));
continue;
}
const [envelope] = envelopes;
const {message, ackId} = envelope;
const copyOfMessageData = psLite.jsonifyMessageData(message);
console.log("message contains:", copyOfMessageData);
await psLite.acknowledge(subName, [ackId]);
isPolling = false;
}
await Promise.all([psLite.deleteSubscription(subName),
psLite.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 |
Promise.<Array.<object>>
createTopic(topicName) ⇒ 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 |
Promise.<string>
deleteTopic(topicName) ⇒ Deletes a Pub/Sub Topic.
Kind: global function
Returns: Promise.<string>
- returns deleted topicName
Param | Type | Description |
---|---|---|
topicName | string |
Name of topic to delete |
Promise.<object>
createSubscription(topicName, subscriptionName, [options]) ⇒ Create a Pub/Sub subscription. Idempotent.
Kind: global function
Returns: Promise.<object>
- returns {success: true}
Param | Type | Default | Description |
---|---|---|---|
topicName | string |
Name of topic to which the subscription attaches. Topic must exist. | |
subscriptionName | string |
Name of subscription to create | |
[options] | object |
{} |
Options passed to GCP Lib's subscriber.createSubscription(). see https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html |
Promise
deleteSubscription(subscriptionName) ⇒ Delete a Pub/Sub subscription.
Kind: global function
Param | Type | Description |
---|---|---|
subscriptionName | string |
Name of subscription to delete |
Promise.<object>
publish(topicName, message) ⇒ 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
Param | Type | Description |
---|---|---|
topicName | string |
Name of topic to publish the message to |
message | PubsubMessage |
Message object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage |
message.data |
buffer | object | string
|
Message data, must be compatible with Buffer.from() https://nodejs.org/docs/latest-v12.x/api/buffer.html |
Promise.<object>
publishJson(topicName, message) ⇒ 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
Param | Type | Description |
---|---|---|
topicName | string |
Name of topic to publish the message to |
message | * |
javascript data to publish. Must be compatible with JSON.stringify() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify |
Promise.<Array.<object>>
pull(subscriptionName, [maxMessages], [returnImmediately]) ⇒ 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 |
Promise
acknowledge(subscriptionName, ackIds) ⇒ 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. |
Promise.<boolean>
subscriptionExists(subscriptionName) ⇒ 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
Param | Type | Description |
---|---|---|
message | receivedMessage |
https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.ReceivedMessage |
Promise.<boolean>
topicExists(topicName) ⇒ Checks whether a topic exists
Kind: global function
Param | Type | Description |
---|---|---|
topicName | string |
name of the topic to check |
string
getProject() ⇒ inspects this module, get what gcp project is being used
PublisherClient
getPublisher() ⇒ inspects this module, get internal google pub/sub publisher client
Kind: global function
Returns: PublisherClient
- https://googleapis.dev/nodejs/pubsub/latest/v1.PublisherClient.html
SubscriberClient
getSubscriber() ⇒ inspects this module, get internal google pub/sub subscriber client
Kind: global function
Returns: SubscriberClient
- https://googleapis.dev/nodejs/pubsub/latest/v1.SubscriberClient.html
Promise.<Array.<PublishResponse>>
publishMany(topicName, messages) ⇒ 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
Param | Type | Description |
---|---|---|
topicName | string |
Name of the topic to publish to |
messages | Array.<PubsubMessage> |
Message objects to publish, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage |
Promise.<Array.<PublishResponse>>
publishManyJson(topicName, messages) ⇒ 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
Param | Type | Description |
---|---|---|
topicName | string |
Name of the topic to publish to |
messages | Array.<object> |
message objects to publish. data value be compatible with JSON.stringify() see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage |
Contributions
Pull Requests are welcome.