diff --git a/functions/node8/index.js b/functions/node8/index.js index ece8dccbbf..bdb9410e0f 100644 --- a/functions/node8/index.js +++ b/functions/node8/index.js @@ -245,3 +245,30 @@ exports.makeUpperCase = (data, context) => { }); }; // [END functions_firebase_reactive_node8] + +// [START functions_async_pubsub_node8] +const PubSub = require('@google-cloud/pubsub'); +let ps; + +/** + * Triggered from a message on a Cloud Pub/Sub topic. + * + * @param {object} data The payload for the Pub/Sub message. + * @param {object} context The context object for the event. + */ +exports.helloPubSub = async (data, context) => { + ps = ps || new PubSub(); + try { + const topicName = context.resource.name; + const topic = ps.topic(topicName); + + // Get metadata for the topic + // This will call the Pub/Sub API + const data = await topic.getMetadata(); + const metadata = data[0]; + console.log(`Metadata: ${JSON.stringify(metadata)}`); + } catch(err) { + console.error(err); + } +}; +// [END functions_async_pubsub_node8] diff --git a/functions/node8/package.json b/functions/node8/package.json index 071c641294..eca154c7ae 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -27,6 +27,7 @@ "uuid": "^3.3.2" }, "dependencies": { - "@google-cloud/firestore": "^0.18.0" + "@google-cloud/firestore": "^0.18.0", + "@google-cloud/pubsub": "^0.20.1" } }