Search code examples
google-cloud-platformgrpcpublish-subscribegoogle-cloud-pubsubgrpc-go

How to make a Push suscription in GCP Pub/Sub with go?


Context:

There are one service in go that uses grcp. In that service there are a method one to one:

rpc CreateOrder (Order) returns (OrderId)

I want to create a push suscription in Google Cloud Pub/sub that invoque this method. I have read the way to make a pull suscription, but i have not found push suscription for gRPC.

Is it possible to create a push suscription via gRPC?


Solution

  • Cloud Pub/Sub is a gRPC-enabled API. It has both REST and RPC interfaces. And Pub/Sub Go uses gRPC.

    To create a push subscription, you need to set the PushConfig parameter inside SubscriptionConfig in the CreateSubscription function, which is already set in the Go code sample from the GCP documentation:

    sub, err := client.CreateSubscription(ctx, subID, pubsub.SubscriptionConfig{
                    Topic:       topic,
                    AckDeadline: 10 * time.Second,
                    PushConfig:  pubsub.PushConfig{Endpoint: endpoint},
    })
    

    And to update a subscription, you need to set the PushConfig parameter inside SubscriptionConfigToUpdate in the update function:

    sub := client.Subscription("subName")
        subConfig, err := sub.Update(ctx, pubsub.SubscriptionConfigToUpdate{
            PushConfig: &pubsub.PushConfig{Endpoint: "https://example.com/push"},
        })