This recipe shows you how to publish messages to a Cloud Pub/Sub topic from a Cloud Function. Where applicable:
Replace [PROJECT-ID] with your Cloud Platform project ID
-
Follow the Cloud Functions quickstart guide to setup Cloud Functions for your project
-
Clone this repository
cd ~/ git clone https://github.com/jasonpolites/gcf-recipes.git cd gcf-recipes/pubsub
-
Create a Cloud Pub/Sub topic (if you already have one you want to use, you can skip this step):
gcloud alpha pubsub topics create gcf-recipes-topic
-
Create a Cloud Storage Bucket to stage our deployment
gsutil mb gs://[PROJECT-ID]-gcf-recipes-bucket
-
Deploy the "publish" function with an HTTP trigger
gcloud alpha functions deploy publish --bucket [PROJECT-ID]-gcf-recipes-bucket --trigger-http
-
Deploy the "subscribe" function with the Pub/Sub topic as a trigger
gcloud alpha functions deploy subscribe --bucket [PROJECT-ID]-gcf-recipes-bucket --trigger-topic gcf-recipes-topic
-
Call the "publish" function
gcloud alpha functions call publish --data '{"topic": "gcf-recipes-topic", "message": "Hello World!"}'
-
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
This recipe comes with a suite of unit tests. To run the tests locally, just use npm test
npm install
npm test
The tests will also produce code coverage reports, written to the /coverage
directory. After running the tests, you can view coverage with
open coverage/lcov-report/index.html