Cloud-Pub-Sub

Create a Function

    Create a Directory and move in that directory

    mkdir gcf_hello
    cd gcf_hello

    create and open index.js

    nano index.js

Create a cloud storage bucket

    gsutil mb -p [Project ID] gs://{bucket name}

Deploy your function

    Deploy the function to a pub/sub topic named hello_world

    gcloud functions deploy helloWorld \
    --stage-bucket [BUCKET_NAME] \
    --trigger-topic hello_world \
    --runtime nodejs8

    Verify the status of the function.

    gcloud functions describe helloWorld

Test the function

    Create a message test of the function.

    DATA=$(printf 'Hello World!'|base64) && gcloud functions call helloWorld --data '{"data":"'$DATA'"}'

View logs

    gcloud functions logs read helloWorld

Pub/Sub topics

    Create a pub/sub Topic called Arish

    gcloud pubsub topics create Arish

    To see the topics

    gcloud pubsub topics list

    Deleting Topics Arish

    gcloud pubsub topics delete Arish

Pub/Sub subscriptions

    Create a subscription called mySubscription

    gcloud pubsub subscriptions create --topic Arish mySubscription

    cmd to list all the subscriptions

    gcloud pubsub topics list-subscriptions Arish

    Delete mySubscriptions

    gcloud pubsub subscriptions delete mySubscriptions

Pub/Sub Publishing and Pulling a Single Message

    Publish the Message "Hello" to the topic you created previously(Arish)

    gcloud pubsub topics publish Arish --message "Hello"

    to pull the messages you just published from the Pub/Sub topic

    gcloud pubsub subscriptions pull mySubscription --auto-ack

    Now you can publish more messages and then pull them. But on pulling only one message will be output.

    • Using the pull command without any flags will output only one message, even if you are subscribed to a topic that has more held in it.

Pub/Sub pulling all the messages from subscriptions

    Add message to your Topic

    gcloud pubsub topics publish Arish --message "Try1"

    gcloud pubsub topics publish Arish --message "Try2"

    Add a flag to your command so you can output all two messages in one request. You may have not noticed, but you have actually been using a flag this entire time: the --auto-ack part of the pull command is a flag that has been formatting your messages into the neat boxes that you see your pulled messages in.

    limit is another flag that sets an upper limit on the number of messages to pull.

    Wait a minute to let the topics get created. Run the pull command with the limit flag:

    gcloud pubsub subscriptions pull mySubscription --auto-ack --limit=2