'use strict'; const {PubSub} = require('@google-cloud/pubsub'); const {Logging} = require('@google-cloud/logging'); const pubsub = new PubSub(); const logging = new Logging(); exports.publish = (req, res) => { const random = Math.random(); // Uncomment for custom logging const log = logging.log("my-log"); const entry = log.entry({resource: { type: 'global', labels: { project_id: process.env.GCLOUD_PROJECT, } }}, 'Hello World'); log.write(entry); console.log(`Wrote a log`); const message = JSON.stringify({ message: random.toString(36).substring(2, 15), }); console.log('Publishing message "'+message+'" to topic'); return pubsub .topic('projects/toto-228009/topics/example') .publisher() .publish(Buffer.from(message)) .then(() => res.status(200).send('Message published.')) .catch(err => { console.error(err); res.status(500).send(err); return Promise.reject(err); }); };