How to use @google-cloud/pubsub - 10 common examples

To help you get started, we’ve selected a few @google-cloud/pubsub examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mozilla / fxa / packages / fxa-event-broker / bin / worker.ts View on Github external
async function main() {
  const capabilityService = new ClientCapabilityService(
    logger,
    Config.get('clientCapabilityFetch')
  );
  const webhookService = new ClientWebhookService(
    logger,
    Config.get('clientCapabilityFetch.refreshInterval'),
    db
  );
  const pubsub = new PubSub();

  // Extract region for SQS object
  const serviceNotificationQueueUrl = Config.get('serviceNotificationQueueUrl');
  const region = extractRegionFromUrl(serviceNotificationQueueUrl);
  if (!region) {
    logger.error('invalidServiceUrl', {
      message: 'Cant find region in service url',
      serviceNotificationQueueUrl
    });
    process.exit(8);
  }
  const metrics = new StatsD(Config.get('metrics'));
  const processor = new ServiceNotificationProcessor(
    logger,
    db,
    metrics,
github googleapis / nodejs-pubsub / samples / topics.js View on Github external
async function getTopicPolicy(topicName) {
  // [START pubsub_get_topic_policy]
  // Imports the Google Cloud client library
  const {PubSub} = require('@google-cloud/pubsub');

  // Creates a client
  const pubsub = new PubSub();

  /**
   * TODO(developer): Uncomment the following line to run the sample.
   */
  // const topicName = 'my-topic';

  // Retrieves the IAM policy for the topic
  const [policy] = await pubsub.topic(topicName).iam.getPolicy();
  console.log(`Policy for topic: %j.`, policy.bindings);

  // [END pubsub_get_topic_policy]
}
github googleapis / nodejs-pubsub / samples / topics.js View on Github external
async function publishBatchedMessages(
  topicName,
  data,
  maxMessages,
  maxWaitTime
) {
  // [START pubsub_publisher_batch_settings]
  // Imports the Google Cloud client library
  const {PubSub} = require('@google-cloud/pubsub');

  // Creates a client
  const pubsub = new PubSub();

  /**
   * TODO(developer): Uncomment the following lines to run the sample.
   */
  // const topicName = 'my-topic';
  // const data = JSON.stringify({ foo: 'bar' });
  // const maxMessages = 10;
  // const maxWaitTime = 10000;

  // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
  const dataBuffer = Buffer.from(data);

  const batchPublisher = pubsub.topic(topicName, {
    batching: {
      maxMessages: maxMessages,
      maxMilliseconds: maxWaitTime,
github googleapis / nodejs-dlp / samples / risk.js View on Github external
callingProjectId,
  tableProjectId,
  datasetId,
  tableId,
  topicId,
  subscriptionId,
  quasiIds
) {
  // [START dlp_k_anonymity]
  // Import the Google Cloud client libraries
  const DLP = require('@google-cloud/dlp');
  const {PubSub} = require('@google-cloud/pubsub');

  // Instantiates clients
  const dlp = new DLP.DlpServiceClient();
  const pubsub = new PubSub();

  // The project ID to run the API call under
  // const callingProjectId = process.env.GCLOUD_PROJECT;

  // The project ID the table is stored under
  // This may or (for public datasets) may not equal the calling project ID
  // const tableProjectId = process.env.GCLOUD_PROJECT;

  // The ID of the dataset to inspect, e.g. 'my_dataset'
  // const datasetId = 'my_dataset';

  // The ID of the table to inspect, e.g. 'my_table'
  // const tableId = 'my_table';

  // The name of the Pub/Sub topic to notify once the job completes
  // TODO(developer): create a Pub/Sub topic to use for this
github googleapis / nodejs-dlp / samples / inspect.js View on Github external
kind,
  topicId,
  subscriptionId,
  minLikelihood,
  maxFindings,
  infoTypes,
  customInfoTypes
) {
  // [START dlp_inspect_datastore]
  // Import the Google Cloud client libraries
  const DLP = require('@google-cloud/dlp');
  const {PubSub} = require('@google-cloud/pubsub');

  // Instantiates clients
  const dlp = new DLP.DlpServiceClient();
  const pubsub = new PubSub();

  // The project ID to run the API call under
  // const callingProjectId = process.env.GCLOUD_PROJECT;

  // The project ID the target Datastore is stored under
  // This may or may not equal the calling project ID
  // const dataProjectId = process.env.GCLOUD_PROJECT;

  // (Optional) The ID namespace of the Datastore document to inspect.
  // To ignore Datastore namespaces, set this to an empty string ('')
  // const namespaceId = '';

  // The kind of the Datastore entity to inspect.
  // const kind = 'Person';

  // The minimum likelihood required before returning a match
github billziss-gh / pmci / controller / index.js View on Github external
],
        "metadata":
        {
            "items":
            [
                {
                    "key": "startup-script",
                    "value": package.config.NETBSD_BUILDER_STARTX,
                },
            ],
        },
    },
}

const pubcli = new pubsub.v1.PublisherClient()
const subcli = new pubsub.v1.SubscriberClient()

exports.listener = (req, rsp) =>
{
    // check if the request has our secret
    if (req.query.secret != package.config.SECRET)
    {
        rsp.status(400).send("You don't know the password!")
        return
    }

    if (req.query.image === undefined ||
        req.query.token === undefined)
    {
        rsp.status(400).send("query: missing image or token")
        return
    }
github billziss-gh / pmci / controller / index.js View on Github external
},
        ],
        "metadata":
        {
            "items":
            [
                {
                    "key": "startup-script",
                    "value": package.config.NETBSD_BUILDER_STARTX,
                },
            ],
        },
    },
}

const pubcli = new pubsub.v1.PublisherClient()
const subcli = new pubsub.v1.SubscriberClient()

exports.listener = (req, rsp) =>
{
    // check if the request has our secret
    if (req.query.secret != package.config.SECRET)
    {
        rsp.status(400).send("You don't know the password!")
        return
    }

    if (req.query.image === undefined ||
        req.query.token === undefined)
    {
        rsp.status(400).send("query: missing image or token")
        return
github googleapis / nodejs-pubsub / samples / subscriptions.js View on Github external
async function synchronousPull(projectName, subscriptionName) {
  // [START pubsub_subscriber_sync_pull]
  // Imports the Google Cloud client library
  const pubsub = require('@google-cloud/pubsub');

  const client = new pubsub.v1.SubscriberClient();

  /**
   * TODO(developer): Uncomment the following lines to run the sample.
   */
  // const projectName = 'your-project';
  // const subscriptionName = 'your-subscription';

  const formattedSubscription = client.subscriptionPath(
    projectName,
    subscriptionName
  );
  // The maximum number of messages returned for this request.
  // Pub/Sub may return fewer than the number specified.
  const maxMessages = 1;
  const newAckDeadlineSeconds = 30;
  const request = {
github googleapis / nodejs-pubsub / samples / topics.js View on Github external
async function publishWithRetrySettings(projectId, topicName, data) {
  // [START pubsub_publisher_retry_settings]
  // Imports the Google Cloud client library
  const {v1} = require('@google-cloud/pubsub');

  // Creates a publisher client
  const client = new v1.PublisherClient({
    // optional auth parameters
  });

  /**
   * TODO(developer): Uncomment the following lines to run the sample.
   */
  // const projectId = 'my-project-id'
  // const topicName = 'my-topic';
  // const data = JSON.stringify({ foo: 'bar' });

  const formattedTopic = client.topicPath(projectId, topicName);
  // Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
  const dataBuffer = Buffer.from(data);
  const messagesElement = {
    data: dataBuffer,
  };
github GoogleCloudPlatform / node-red-contrib-google-cloud / tests / pubsub_in_node_spec.js View on Github external
describe('pubsub_in Node', () => {

    const pubsub = new PubSub();

    beforeEach((done) => {
        helper.startServer(done);
    });

    afterEach((done)=> {
        helper.unload();
        helper.stopServer(done);
    });

    it('receive pubsub message', (done) => {
        const flow = [
            {
                "id": "n1",
                "type": "google-cloud-pubsub in",
                "subscription": "node-red-subscription",

@google-cloud/pubsub

Cloud Pub/Sub Client Library for Node.js

Apache-2.0
Latest version published 2 months ago

Package Health Score

91 / 100
Full package analysis