v1 Publish messages with compression rate (DEPRECATED)

(DEPRECATED) Publish messages with compression rate

Code sample

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" 
 ) 
 func 
  
 publishWithCompression 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 // msg := "Hello World" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub: NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 // Enable compression and configure the compression threshold to 10 bytes (default to 240 B). 
  
 // Publish requests of sizes > 10 B (excluding the request headers) will get compressed. 
  
 t 
 . 
 PublishSettings 
 . 
 EnableCompression 
  
 = 
  
 true 
  
 t 
 . 
 PublishSettings 
 . 
 CompressionBytesThreshold 
  
 = 
  
 10 
  
 result 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 [] 
 byte 
 ( 
 "This is a test message" 
 ), 
  
 }) 
  
 // Block until the result is returned and a server-generated 
  
 // ID is returned for the published message. 
  
 id 
 , 
  
 err 
  
 := 
  
 result 
 . 
 Get 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub: result.Get: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Published a message; msg ID: %v\n" 
 , 
  
 id 
 ) 
  
 return 
  
 nil 
 } 
 

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 .

  require 
  
 "google/cloud/pubsub" 
 ## 
 # Shows how to create a BigQuery subscription where messages published 
 # to a topic populates a BigQuery table. 
 # 
 # @param project_id [String] 
 # Your Google Cloud project (e.g. "my-project") 
 # @param topic_id [String] 
 # Your topic name (e.g. "my-secret") 
 # 
 def 
  
 pubsub_old_version_publisher_with_compression 
  
 project_id 
 :, 
  
 topic_id 
 : 
  
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
  
 project_id 
 : 
  
 project_id 
  
 # Enable compression and configure the compression threshold to 10 bytes (default to 240 B). 
  
 # Publish requests of sizes > 10 B (excluding the request headers) will get compressed. 
  
 topic 
  
 = 
  
 pubsub 
 . 
 topic 
  
 topic_id 
 , 
  
 async 
 : 
  
 { 
  
 compress 
 : 
  
 true 
 , 
  
 compression_bytes_threshold 
 : 
  
 10 
  
 } 
  
 begin 
  
 topic 
 . 
 publish_async 
  
 "This is a test message." 
  
 do 
  
 | 
 result 
 | 
  
 raise 
  
 "Failed to publish the message." 
  
 unless 
  
 result 
 . 
 succeeded? 
  
 puts 
  
 "Published a compressed message of message ID: 
 #{ 
 result 
 . 
 message_id 
 } 
 " 
  
 end 
  
 # Stop the async_publisher to send all queued messages immediately. 
  
 topic 
 . 
 async_publisher 
 . 
 stop 
 . 
 wait! 
  
 rescue 
  
 StandardError 
  
 = 
>  
 e 
  
 puts 
  
 "Received error while publishing: 
 #{ 
 e 
 . 
 message 
 } 
 " 
  
 end 
 end 
 

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: