google-cloud-pubsub

Google Cloud Pub/Sub ( docs ) is designed to provide reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a “topic” and other applications can subscribe to that topic to receive the messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows developers to communicate between independently written applications.

Quick Start

 $ gem install google-cloud-pubsub 

Authentication

This library uses Service Account credentials to connect to Google Cloud services. When running on Google Cloud Platform (GCP), including Google Compute Engine (GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud Functions (GCF) and Cloud Run, the credentials will be discovered automatically. When running on other environments the Service Account credentials can be specified by providing the path to the JSON file, or the JSON itself, in environment variables.

Instructions and configuration options are covered in the Authentication Guide .

Example

 require 
  
 "googleauth" 
 require 
  
 "google/cloud/pubsub" 
 credentials 
  
 = 
  
 :: 
 Google 
 :: 
 Auth 
 :: 
 ServiceAccountCredentials 
 . 
 make_creds 
 ( 
  
 json_key_io 
 : 
  
 :: 
 File 
 . 
 open 
 ( 
 "/path/to/keyfile.json" 
 ) 
 ) 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
 PubSub 
 . 
 new 
 ( 
  
 project_id 
 : 
  
 "my-project" 
 , 
  
 credentials 
 : 
  
 credentials 
 ) 
 # Get a publisher for a topic 
 publisher 
  
 = 
  
 pubsub 
 . 
 publisher 
  
 "my-topic" 
 # Publish a new message 
 msg 
  
 = 
  
 publisher 
 . 
 publish 
  
 "new-message" 
 # Get a subscriber for a subscription 
 subscriber 
  
 = 
  
 pubsub 
 . 
 subscriber 
  
 "my-topic-sub" 
 # Create a listener to listen for available messages 
 # By default, this block will be called on 8 concurrent threads. 
 # This can be changed with the :threads option 
 listener 
  
 = 
  
 subscriber 
 . 
 listen 
  
 do 
  
 | 
 received_message 
 | 
  
 # process message 
  
 puts 
  
 "Data: 
 #{ 
 received_message 
 . 
 message 
 . 
 data 
 } 
 , published at 
 #{ 
 received_message 
 . 
 message 
 . 
 published_at 
 } 
 " 
  
 received_message 
 . 
 acknowledge! 
 end 
 # Handle exceptions from listener 
 listener 
 . 
 on_error 
  
 do 
  
 | 
 exception 
 | 
  
 puts 
  
 "Exception: 
 #{ 
 exception 
 . 
 class 
 } 
  
 #{ 
 exception 
 . 
 message 
 } 
 " 
 end 
 # Gracefully shut down the subscriber on program exit, blocking until 
 # all received messages have been processed or 10 seconds have passed 
 at_exit 
  
 do 
  
 listener 
 . 
 stop! 
 ( 
 10 
 ) 
 end 
 # Start background threads that will call the block passed to listen. 
 listener 
 . 
 start 
 # Block, letting processing threads continue in the background 
 sleep 

Enabling Logging

To enable logging for this library, set the logger for the underlying gRPC library. The logger that you set may be a Ruby stdlib Logger as shown below, or a Google::Cloud::Logging::Logger that will write logs to Stackdriver Logging . See grpc/logconfig.rb and the gRPC spec_helper.rb for additional information.

Configuring a Ruby stdlib logger:

 require 
  
 "logger" 
 module 
  
 MyLogger 
  
 LOGGER 
  
 = 
  
 Logger 
 . 
 new 
  
 $stderr 
 , 
  
 level 
 : 
  
 Logger 
 :: 
 WARN 
  
 def 
  
 logger 
  
 LOGGER 
  
 end 
 end 
 # Define a gRPC module-level logger method before grpc/logconfig.rb loads. 
 module 
  
 GRPC 
  
 extend 
  
 MyLogger 
 end 

Enabling library level logging

This library includes an opt-in logging mechanism that provides detailed information about high-level operations. These logs are useful for troubleshooting and monitoring the client's behavior. When enabled, logs are tagged with subtags to indicate the operation type.

The following subtags are used:

  • callback-delivery : Logs when a message is delivered to the user-provided callback.
  • callback-exceptions : Logs any exceptions raised from the user callback.
  • ack-nack : Logs when a message is acknowledged ( ack ) or negatively acknowledged ( nack ).
  • ack-batch : Logs the reason and size of acknowledgement batches sent to the server.
  • publish-batch : Logs the reason and size of message batches sent to the server for publishing.
  • expiry : Logs when a message's lease expires and it is dropped from client-side lease management.
  • subscriber-streams : Logs key events in the subscriber's streaming connection, such as opening, closing, and errors.
  • subscriber-flow-control : Logs when the subscriber's client-side flow control is paused or resumed.

WARNING:These logs may contain message data in plaintext, which could include sensitive information. Ensure you are practicing good data hygiene with your application logs. It is recommended to enable this logging only for debugging purposes and not permanently in production.

To enable these debug logs, you must provide a logger with the progname set to "pubsub" .

 require 
  
 "google/cloud/pubsub" 
 require 
  
 "logger" 
 # Create a logger and set the progname to "pubsub" to enable library-level logging 
 logger 
  
 = 
  
 Logger 
 . 
 new 
 ( 
 $stdout 
 ) 
 logger 
 . 
 progname 
  
 = 
  
 "pubsub" 
 # Configure the logger globally 
 Google 
 :: 
 Cloud 
 . 
 configure 
 . 
 pubsub 
 . 
 logger 
  
 = 
  
 logger 
 # Or provide it directly to the client 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
 PubSub 
 . 
 new 
  
 logger 
 : 
  
 logger 

If the logger's progname is not set to "pubsub" , these debug logs will be suppressed, even if the logger is provided.

Supported Ruby Versions

This library is supported on Ruby 3.1+.

Google provides official support for Ruby versions that are actively supported by Ruby Core—that is, Ruby versions that are either in normal maintenance or in security maintenance, and not end of life. Older versions of Ruby may still work, but are unsupported and not recommended. See https://www.ruby-lang.org/en/downloads/branches/ for details about the Ruby support schedule.

Versioning

This library follows Semantic Versioning .

This library is considered to be stable and will not have backwards-incompatible changes introduced in subsequent minor releases.

Contributing

Contributions to this library are always welcome and highly encouraged.

See the Contributing Guide for more information on how to get started.

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. See Code of Conduct for more information.

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE .

Support

Please report bugs at the project on Github . Don't hesitate to ask questions about the client or APIs on StackOverflow .

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