Update subscription

Updates a subscription.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

Before trying this sample, follow the C++ setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C++ API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  namespace 
  
 pubsub_admin 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub_admin 
 ; 
 namespace 
  
 pubsub 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub 
 ; 
 []( 
 pubsub_admin 
 :: 
 SubscriptionAdminClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 subscription_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 endpoint 
 ) 
  
 { 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 ModifyPushConfigRequest 
  
 request 
 ; 
  
 request 
 . 
 set_subscription 
 ( 
  
 pubsub 
 :: 
 Subscription 
 ( 
 project_id 
 , 
  
 subscription_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_push_config 
 () 
 - 
> set_push_endpoint 
 ( 
 endpoint 
 ); 
  
 auto 
  
 status 
  
 = 
  
 client 
 . 
 ModifyPushConfig 
 ( 
 request 
 ); 
  
 if 
  
 ( 
 ! 
 status 
 . 
 ok 
 ()) 
  
 throw 
  
 std 
 :: 
 runtime_error 
 ( 
 status 
 . 
 message 
 ()); 
  
 std 
 :: 
 cout 
 << 
 "The subscription push configuration was successfully" 
 << 
 " modified 
 \n 
 " 
 ; 
 } 
 

C#

Before trying this sample, follow the C# setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C# API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 public 
  
 class 
  
 UpdatePushConfigurationSample 
 { 
  
 public 
  
 void 
  
 UpdatePushConfiguration 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 subscriptionId 
 , 
  
 string 
  
 pushEndpoint 
 ) 
  
 { 
  
  SubscriberServiceApiClient 
 
  
 subscriber 
  
 = 
  
  SubscriberServiceApiClient 
 
 . 
  Create 
 
 (); 
  
  SubscriptionName 
 
  
 subscriptionName 
  
 = 
  
  SubscriptionName 
 
 . 
  FromProjectSubscription 
 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
  PushConfig 
 
  
 pushConfig 
  
 = 
  
 new 
  
  PushConfig 
 
  
 { 
  
 PushEndpoint 
  
 = 
  
 pushEndpoint 
  
 }; 
  
 subscriber 
 . 
  ModifyPushConfig 
 
 ( 
 subscriptionName 
 , 
  
 pushConfig 
 ); 
  
 } 
 } 
 

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "cloud.google.com/go/pubsub/v2" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
  
 "google.golang.org/protobuf/types/known/fieldmaskpb" 
 ) 
 func 
  
 updateEndpoint 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 subscriptionName 
 , 
  
 endpoint 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // subscriptionName := "projects/my-project/subscriptions/my-sub" 
  
 // endpoint := "https://my-test-project.appspot.com/push" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 UpdateSubscriptionRequest 
 { 
  
 Subscription 
 : 
  
& pubsubpb 
 . 
 Subscription 
 { 
  
 Name 
 : 
  
 subscriptionName 
 , 
  
 PushConfig 
 : 
  
& pubsubpb 
 . 
 PushConfig 
 { 
  
 PushEndpoint 
 : 
  
 endpoint 
 , 
  
 }, 
  
 }, 
  
 UpdateMask 
 : 
  
& fieldmaskpb 
 . 
 FieldMask 
 { 
  
 Paths 
 : 
  
 [] 
 string 
 { 
 "push_config" 
 }, 
  
 }, 
  
 } 
  
 subConfig 
 , 
  
 err 
  
 := 
  
 client 
 . 
 SubscriptionAdminClient 
 . 
 UpdateSubscription 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "Update: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Updated subscription config: %v\n" 
 , 
  
 subConfig 
 ) 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Java API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.pubsub.v1. SubscriptionAdminClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. PushConfig 
 
 ; 
 import 
  
 com.google.pubsub.v1. Subscription 
 
 ; 
 import 
  
 com.google.pubsub.v1. SubscriptionName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 UpdatePushConfigurationExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 String 
  
 subscriptionId 
  
 = 
  
 "your-subscription-id" 
 ; 
  
 String 
  
 pushEndpoint 
  
 = 
  
 "https://my-test-project.appspot.com/push" 
 ; 
  
 updatePushConfigurationExample 
 ( 
 projectId 
 , 
  
 subscriptionId 
 , 
  
 pushEndpoint 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 updatePushConfigurationExample 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 subscriptionId 
 , 
  
 String 
  
 pushEndpoint 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 try 
  
 ( 
  SubscriptionAdminClient 
 
  
 subscriptionAdminClient 
  
 = 
  
  SubscriptionAdminClient 
 
 . 
 create 
 ()) 
  
 { 
  
  SubscriptionName 
 
  
 subscriptionName 
  
 = 
  
  SubscriptionName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 subscriptionId 
 ); 
  
  PushConfig 
 
  
 pushConfig 
  
 = 
  
  PushConfig 
 
 . 
 newBuilder 
 (). 
  setPushEndpoint 
 
 ( 
 pushEndpoint 
 ). 
 build 
 (); 
  
 subscriptionAdminClient 
 . 
 modifyPushConfig 
 ( 
 subscriptionName 
 , 
  
 pushConfig 
 ); 
  
  Subscription 
 
  
 subscription 
  
 = 
  
 subscriptionAdminClient 
 . 
 getSubscription 
 ( 
 subscriptionName 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
  
 "Updated push endpoint to: " 
  
 + 
  
 subscription 
 . 
  getPushConfig 
 
 (). 
 getPushEndpoint 
 ()); 
  
 } 
  
 } 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  /** 
 * TODO(developer): Uncomment these variables before running the sample. 
 */ 
 // const topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID'; 
 // const subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID'; 
 // Imports the Google Cloud client library 
 const 
  
 { 
 PubSub 
 } 
  
 = 
  
 require 
 ( 
 '@ 
 google 
 - 
 cloud 
 / 
 pubsub 
 ' 
 ); 
 // Creates a client; cache this for further use 
 const 
  
 pubSubClient 
  
 = 
  
 new 
  
 PubSub 
 (); 
 async 
  
 function 
  
 modifyPushConfig 
 ( 
 topicNameOrId 
 , 
  
 subscriptionNameOrId 
 ) 
  
 { 
  
 const 
  
 options 
  
 = 
  
 { 
  
 // Set to an HTTPS endpoint of your choice. If necessary, register 
  
 // (authorize) the domain on which the server is hosted. 
  
 pushEndpoint 
 : 
  
 ` 
 https 
 : 
 //${pubSubClient.projectId}.appspot.com/push`, 
  
 }; 
  
 await 
  
 pubSubClient 
  
 . 
 topic 
 ( 
 topicNameOrId 
 ) 
  
 . 
 subscription 
 ( 
 subscriptionNameOrId 
 ) 
  
 . 
 modifyPushConfig 
 ( 
 options 
 ); 
  
 console 
 . 
 log 
 ( 
 ` 
 Modified 
  
 push 
  
 config 
  
 for 
  
 subscription 
  
 $ 
 { 
 subscriptionNameOrId 
 }. 
 ` 
 ); 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  /** 
 * 
 TODO 
 ( 
 developer 
 ): 
 Uncomment 
 these 
 variables 
 before 
 running 
 the 
 sample 
 . 
 */ 
 // 
 const 
 topicNameOrId 
 = 
 'YOUR_TOPIC_NAME_OR_ID' 
 ; 
 // 
 const 
 subscriptionNameOrId 
 = 
 'YOUR_SUBSCRIPTION_NAME_OR_ID' 
 ; 
 // 
 Imports 
 the 
 Google 
 Cloud 
 client 
 library 
 import 
  
 { 
 PubSub 
 , 
 CreateSubscriptionOptions 
 } 
 from 
  
 '@google-cloud/pubsub' 
 ; 
 // 
 Creates 
 a 
 client 
 ; 
 cache 
 this 
 for 
 further 
 use 
 const 
 pubSubClient 
 = 
 new 
 PubSub 
 (); 
 async 
 function 
 modifyPushConfig 
 ( 
 topicNameOrId 
 : 
 string 
 , 
 subscriptionNameOrId 
 : 
 string 
 , 
 ) 
 { 
 const 
 options 
 : 
 CreateSubscriptionOptions 
 = 
 { 
 // 
 Set 
 to 
 an 
 HTTPS 
 endpoint 
 of 
 your 
 choice 
 . 
 If 
 necessary 
 , 
 register 
 // 
 ( 
 authorize 
 ) 
 the 
 domain 
 on 
 which 
 the 
 server 
 is 
 hosted 
 . 
 pushEndpoint 
 : 
 ` 
 https 
 : 
 // 
 $ 
 { 
 pubSubClient 
 . 
 projectId 
 } 
 . 
 appspot 
 . 
 com 
 / 
 push 
 ` 
 , 
 }; 
 await 
 pubSubClient 
 . 
 topic 
 ( 
 topicNameOrId 
 ) 
 . 
 subscription 
 ( 
 subscriptionNameOrId 
 ) 
 . 
 modifyPushConfig 
 ( 
 options 
 ); 
 console 
 . 
 log 
 ( 
 ` 
 Modified 
 push 
 config 
 for 
 subscription 
 $ 
 { 
 subscriptionNameOrId 
 } 
 . 
 ` 
 ); 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Python API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.cloud 
  
 import 
 pubsub_v1 
 # TODO(developer) 
 # project_id = "your-project-id" 
 # topic_id = "your-topic-id" 
 # subscription_id = "your-subscription-id" 
 # endpoint = "https://my-test-project.appspot.com/push" 
 subscriber 
 = 
 pubsub_v1 
 . 
  SubscriberClient 
 
 () 
 subscription_path 
 = 
 subscriber 
 . 
 subscription_path 
 ( 
 project_id 
 , 
 subscription_id 
 ) 
 push_config 
 = 
 pubsub_v1 
 . 
 types 
 . 
  PushConfig 
 
 ( 
 push_endpoint 
 = 
 endpoint 
 ) 
 subscription 
 = 
 pubsub_v1 
 . 
 types 
 . 
  Subscription 
 
 ( 
 name 
 = 
 subscription_path 
 , 
 topic 
 = 
 topic_id 
 , 
 push_config 
 = 
 push_config 
 ) 
 update_mask 
 = 
 { 
 "paths" 
 : 
 { 
 "push_config" 
 }} 
 # Wrap the subscriber in a 'with' block to automatically call close() to 
 # close the underlying gRPC channel when done. 
 with 
 subscriber 
 : 
 result 
 = 
 subscriber 
 . 
 update_subscription 
 ( 
 request 
 = 
 { 
 "subscription" 
 : 
 subscription 
 , 
 "update_mask" 
 : 
 update_mask 
 } 
 ) 
 print 
 ( 
 f 
 "Subscription updated: 
 { 
 subscription_path 
 } 
 " 
 ) 
 print 
 ( 
 f 
 "New endpoint for subscription is: 
 { 
 result 
 . 
 push_config 
 } 
 ." 
 ) 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # subscription_id   = "your-subscription-id" 
 # new_endpoint      = "Endpoint where your app receives messages"" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 subscription_admin 
  
 = 
  
 pubsub 
 . 
  subscription_admin 
 
 subscription 
  
 = 
  
 subscription_admin 
 . 
 get_subscription 
  
 \ 
  
 subscription 
 : 
  
 pubsub 
 . 
 subscription_path 
 ( 
 subscription_id 
 ) 
 subscription 
 . 
 push_config 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 :: 
  V1 
 
 :: 
 PushConfig 
 . 
  new 
 
  
 \ 
  
 push_endpoint 
 : 
  
 new_endpoint 
 subscription_admin 
 . 
 update_subscription 
  
 subscription 
 : 
  
 subscription 
 , 
  
 update_mask 
 : 
  
 { 
  
 paths 
 : 
  
 [ 
 "push_config" 
 ] 
  
 } 
 puts 
  
 "Push endpoint updated." 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: