Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@pluralsight/gcp-pubsub-lite

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@pluralsight/gcp-pubsub-lite

Convenience wrapper for GCP Pub/Sub

  • 1.0.15
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14
increased by1300%
Maintainers
1
Weekly downloads
 
Created
Source

gcp-pubsub-lite

npm version GitHub Workflow Status

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

ParamTypeDescription
gcpPubSubobjectGoogle Pub/Sub Library from require("@google-cloud/pubsub")
projectIdstringGCP 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

ParamTypeDescription
topicNamestringName of topic to create

deleteTopic(topicName) ⇒ Promise.<string>

Deletes a Pub/Sub Topic.

Kind: global function
Returns: Promise.<string> - returns deleted topicName

ParamTypeDescription
topicNamestringName 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}

ParamTypeDefaultDescription
topicNamestringName of topic to which the subscription attaches. Topic must exist.
subscriptionNamestringName of subscription to create
[options]object{}Options passed to GCP Lib's subscriber.createSubscription(). see https://googleapis.github.io/gax-nodejs/interfaces/CallOptions.html

deleteSubscription(subscriptionName) ⇒ Promise

Delete a Pub/Sub subscription.

Kind: global function

ParamTypeDescription
subscriptionNamestringName 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

ParamTypeDescription
topicNamestringName of topic to publish the message to
messagePubsubMessageMessage object, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage
message.databuffer | object | stringMessage data, must be compatible with Buffer.from() https://nodejs.org/docs/latest-v12.x/api/buffer.html

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

ParamTypeDescription
topicNamestringName 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

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

ParamTypeDefaultDescription
subscriptionNamestringName of subscription
[maxMessages]number1Maximum number of messages to pull
[returnImmediately]booleantrueWhether 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

ParamTypeDescription
subscriptionNamestringName of subscription
ackIdsArray.<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

ParamTypeDescription
subscriptionNamestringName 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

ParamTypeDescription
messagereceivedMessagehttps://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.ReceivedMessage

topicExists(topicName) ⇒ Promise.<boolean>

Checks whether a topic exists

Kind: global function

ParamTypeDescription
topicNamestringname 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

ParamTypeDescription
topicNamestringName of the topic to publish to
messagesArray.<PubsubMessage>Message objects to publish, see https://googleapis.dev/nodejs/pubsub/latest/google.pubsub.v1.html#.PubsubMessage

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

ParamTypeDescription
topicNamestringName of the topic to publish to
messagesArray.<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.

Keywords

FAQs

Package last updated on 04 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc