Cloud Pub/Sub API - Class Google::Cloud::PubSub::Message (v2.10.0)

Reference documentation and code samples for the Cloud Pub/Sub API 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.

Inherits

  • Object

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!

Methods

#attributes

def attributes()

Optional attributes for the message.

#data

def data()

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

#initialize

def initialize(data = nil, attributes = {}) -> Message

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

Returns
  • (Message) — a new instance of Message

#message_id

def message_id()
Aliases

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

#msg_id

def msg_id()
Alias Of: #message_id

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

#ordering_key

def ordering_key() -> String

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.

Returns
  • (String)

#publish_time

def publish_time()
Alias Of: #published_at

The time at which the message was published.

#published_at

def published_at()
Aliases

The time at which the message was published.