class Google::Cloud::PubSub::Message

# Message

Represents a Pub/Sub Message.

Message objects are created by {Topic#publish}. {Subscription#pull} returns an array of {ReceivedMessage} objects, each of which contains a Message object. Each {ReceivedMessage} object can be acknowledged and/or delayed.

@example

require "google/cloud/pubsub"

pubsub = Google::Cloud::PubSub.new

# Publish a message
topic = pubsub.topic "my-topic"
message = topic.publish "task completed"
message.data #=> "task completed"

# Listen for messages
sub = pubsub.subscription "my-topic-sub"
subscriber = sub.listen do |received_message|
  # process message
  received_message.acknowledge!
end

# Start background threads that will call the block passed to listen.
subscriber.start

# Shut down the subscriber when ready to stop receiving messages.
subscriber.stop!

Attributes

grpc[RW]

@private The gRPC Google::Cloud::PubSub::V1::PubsubMessage object.

Public Class Methods

from_grpc(grpc) click to toggle source

@private New Message from a Google::Cloud::PubSub::V1::PubsubMessage object.

# File lib/google/cloud/pubsub/message.rb, line 148
def self.from_grpc grpc
  new.tap do |m|
    m.instance_variable_set :@grpc, grpc
  end
end
new(data = nil, attributes = {}) click to toggle source

Create an empty Message object. This can be used to publish several messages in bulk.

# File lib/google/cloud/pubsub/message.rb, line 63
def initialize data = nil, attributes = {}
  # Convert attributes to strings to match the protobuf definition
  attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]

  @grpc = Google::Cloud::PubSub::V1::PubsubMessage.new(
    data:       String(data).dup.force_encoding(Encoding::ASCII_8BIT),
    attributes: attributes
  )
end

Public Instance Methods

<=>(other) click to toggle source

@private

# File lib/google/cloud/pubsub/message.rb, line 139
def <=> other
  return nil unless other.is_a? self.class
  other_grpc = other.instance_variable_get :@grpc
  @grpc <=> other_grpc
end
==(other)

@private

Alias for: eql?
attributes() click to toggle source

Optional attributes for the message.

# File lib/google/cloud/pubsub/message.rb, line 82
def attributes
  return @grpc.attributes.to_h if @grpc.attributes.respond_to? :to_h
  # Enumerable doesn't have to_h on Ruby 2.0, so fallback to this
  Hash[@grpc.attributes.to_a]
end
data() click to toggle source

The message payload. This data is a list of bytes encoded as ASCII-8BIT.

# File lib/google/cloud/pubsub/message.rb, line 76
def data
  @grpc.data
end
eql?(other) click to toggle source

@private

# File lib/google/cloud/pubsub/message.rb, line 131
def eql? other
  return false unless other.is_a? self.class
  @grpc.hash == other.hash
end
Also aliased as: ==
hash() click to toggle source

@private

# File lib/google/cloud/pubsub/message.rb, line 126
def hash
  @grpc.hash
end
message_id() click to toggle source

The ID of this message, assigned by the server at publication time. Guaranteed to be unique within the topic.

# File lib/google/cloud/pubsub/message.rb, line 91
def message_id
  @grpc.message_id
end
Also aliased as: msg_id
msg_id()
Alias for: message_id
ordering_key() click to toggle source

Identifies related messages for which publish order should be respected.

Google Cloud Pub/Sub ordering keys provide the ability to ensure related messages are sent to subscribers in the order in which they were published. Messages can be tagged with an ordering key, a string that identifies related messages for which publish order should be respected. The service guarantees that, for a given ordering key and publisher, messages are sent to subscribers in the order in which they were published. Ordering does not require sacrificing high throughput or scalability, as the service automatically distributes messages for different ordering keys across subscribers.

See {Topic#publish_async} and {Subscription#listen}.

@return [String]

# File lib/google/cloud/pubsub/message.rb, line 121
def ordering_key
  @grpc.ordering_key
end
publish_time()
Alias for: published_at
published_at() click to toggle source

The time at which the message was published.

# File lib/google/cloud/pubsub/message.rb, line 98
def published_at
  Convert.timestamp_to_time @grpc.publish_time
end
Also aliased as: publish_time