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 
  
 "google/cloud/pubsub" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
 PubSub 
 . 
 new 
 ( 
  
 project_id 
 : 
  
 "my-project" 
 , 
  
 credentials 
 : 
  
 "/path/to/keyfile.json" 
 ) 
 # 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 

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: