How to use the gcloud.pubsub function in gcloud

To help you get started, we’ve selected a few gcloud 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 jasonpolites / gcf-recipes / worker_pubsub / index.js View on Github external
'worker': function(context, data) {

    // We expect the data argument to contain a 'line' property.
    var batch = data['batch'];

    // Batch should be an array.
    var count = 0;
    for (var i = 0; i < batch.length; i++) {
      var line = batch[i];
      // Just split to count words.
      count += line.split(/\s+/).length;
    }

    // Create a pubsub client to publish the result
    var pubsub = gcloud.pubsub({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    var outTopic = pubsub.topic(data['out-topic']);

    logger.log('Worker ' + data['worker'] + ' reporting total count of ' +
      count + ' in batch of size [' + batch.length + ']');

    outTopic.publish({
      data: {
        count: count,
        worker: data['worker']
      }
    }, function(err) {
      if (err) {
github jasonpolites / gcf-recipes / pubsub / index.js View on Github external
context.failure(
        'Topic not provided. Make sure you have a \'topic\' property in ' +
        'your request');
      return;
    }
    if (!message) {
      context.failure(
        'Message not provided. Make sure you have a \'message\' property ' +
        'in your request');
      return;
    }

    logger.log('Publishing message to topic ' + topicName);

    // Create a pubsub client.
    var pubsub = gcloud.pubsub({
      // We're using the API from the same project as the Cloud Function.
      projectId: process.env.GCP_PROJECT,
    });

    // The Pub/Sub topic must already exist.
    var topic = pubsub.topic(topicName);

    // Pub/Sub messages must be valid JSON objects.
    topic.publish({
        data: {
          message: message,
        },
      },
      function(err) {
        if (err) {
          logger.error(err);
github googleapis / nodejs-pubsub / samples / subscription.js View on Github external
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

var async = require('async');

// [START auth]
// By default, gcloud will authenticate using the service account file specified
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
// project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
var gcloud = require('gcloud');

// Get a reference to the pubsub component
var pubsub = gcloud.pubsub();
// [END auth]

// [START create_topic]
/**
 * @param {string} topicName Name for the new topic.
 * @param {Function} callback Callback function.
 */
function createTopicExample (topicName, callback) {
  var topic = pubsub.topic(topicName);

  // Get the topic if it exists. Create it if it does not exist.
  topic.get({
    autoCreate: true
  }, function (err, topic, apiResponse) {
    if (err) {
      return callback(err);

gcloud

Google Cloud APIs Client Library for Node.js

Apache-2.0
Latest version published 8 years ago

Package Health Score

63 / 100
Full package analysis