Sitelet https://github.com/boro741/nodejs-docs-samples/tree/spanner-bug-fix-node/functions/pubsub
Skip to content

Latest commit

 

History

History
 
 

pubsub

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Google Cloud Platform logo

Google Cloud Functions Pub/Sub sample

This recipe shows you how to publish messages to a Cloud Pub/Sub topic from a Cloud Function.

View the source code.

Deploy and Test

  1. Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project.

  2. Clone this repository:

     git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
     cd nodejs-docs-samples/functions/pubsub
    
  3. Create a Cloud Pub/Sub topic (if you already have one you want to use, you can skip this step):

     gcloud beta pubsub topics create YOUR_TOPIC_NAME
    
    • Replace YOUR_TOPIC_NAME with the name of your Pub/Sub Topic.
  4. Create a Cloud Storage Bucket to stage our deployment:

     gsutil mb gs://YOUR_BUCKET_NAME
    
    • Replace YOUR_BUCKET_NAME with the name of your Cloud Storage Bucket.
  5. Deploy the publish function with an HTTP trigger:

     gcloud alpha functions deploy publish --stage-bucket YOUR_BUCKET_NAME --trigger-http
    
    • Replace YOUR_BUCKET_NAME with the name of your Cloud Storage Bucket.
  6. Deploy the subscribe function with the Pub/Sub topic as a trigger:

     gcloud alpha functions deploy subscribe --stage-bucket YOUR_BUCKET_NAME --trigger-topic YOUR_TOPIC_NAME
    
    • Replace YOUR_BUCKET_NAME with the name of your Cloud Storage Bucket.
    • Replace YOUR_TOPIC_NAME with the name of your Pub/Sub Topic.
  7. Call the publish function:

     gcloud alpha functions call publish --data '{"topic":"YOUR_TOPIC_NAME","message":"Hello World!"}'
    
    • Replace YOUR_TOPIC_NAME with the name of your Pub/Sub Topic.
  8. Check the logs for the subscribe function:

     gcloud alpha functions get-logs subscribe
    

    You should see something like this in your console:

     D      ... User function triggered, starting execution
     I      ... Hello World!
     D      ... Execution took 1 ms, user function completed successfully