"publish" : function (context, data) { var topicName = data['topic']; var message = data['message']; if(!topicName) { context.failure("Topic not provided. Make sure you have a 'topic' property in your request"); } else { if(!message) { context.failure("Message not provided. Make sure you have a 'message' property in your request"); } else { console.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) { context.failure(err); } else { context.success(); } }); } } },
module.exports = function(gcloudConfig, logging) { var pubsub = gcloud.pubsub(config.gcloud); // This configuration will automatically create the topic if // it doesn't yet exist. Usually, you'll want to make sure // that a least one subscription exists on the topic before // publishing anything to it as topics without subscribers // will essentially drop any messages. function getTopic(cb) { pubsub.createTopic(topicName, function(err, topic) { // topic already exists. if (err && err.code === 409) { return cb(null, pubsub.topic(topicName)); } return cb(err, topic); }); } // Used by the worker to listen to pubsub messages. // When more than one worker is running they will all share the same // subscription, which means that pub/sub will evenly distribute messages // to each worker. function subscribe(cb) { getTopic(function(err, topic) { if (err) { return cb(err); } topic.subscribe(subscriptionName, { autoAck: true, reuseExisting: true }, function(err, subscription) { if (err) { return cb(err); } subscription.on('message', function(message) { cb(null, message.data); }); logging.info('Listening to ' + topicName + ' with subscription ' + subscriptionName); }); }); } // Adds a book to the queue to be processed by the worker. function queueBook(bookId) { getTopic(function(err, topic) { if (err) { logging.error('Error occurred while getting pubsub topic', err); return; } topic.publish({ data: { action: 'processBook', bookId: bookId } }, function(err) { if (err) { logging.error('Error occurred while queuing background task', err); } else { logging.info('Book ' + bookId + ' queued for background processing'); } }); }); } return { subscribe: subscribe, queueBook: queueBook }; };
// distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. 'use strict'; var gcloud = require('gcloud'); var config = require('../config'); var logging = require('./logging'); var topicName = 'book-process-queue'; var subscriptionName = 'shared-worker-subscription'; var pubsub = gcloud.pubsub({ projectId: config.get('GCLOUD_PROJECT') }); // This configuration will automatically create the topic if // it doesn't yet exist. Usually, you'll want to make sure // that a least one subscription exists on the topic before // publishing anything to it as topics without subscribers // will essentially drop any messages. function getTopic (cb) { pubsub.createTopic(topicName, function (err, topic) { // topic already exists. if (err && err.code === 409) { return cb(null, pubsub.topic(topicName)); } return cb(err, topic); });
// 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) {
var app = express(); app.set('view engine', 'jade'); var formBodyParser = bodyParser.urlencoded({extended: false}); var jsonBodyParser = bodyParser.json(); // List of all messages received by this instance var messages = []; // The following environment variables are set by app.yaml when running on GAE, // but will need to be manually set when running locally. var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN; var pubsub = gcloud.pubsub({ projectId: process.env.GCLOUD_PROJECT }); var topic = pubsub.topic(process.env.PUBSUB_TOPIC); // [START index] app.get('/', function (req, res) { res.render('index', { messages: messages }); }); app.post('/', formBodyParser, function (req, res, next) { if (!req.body.payload) { return res.status(400).send('Missing payload'); } topic.publish({