require"google/cloud/pubsub"pubsub=Google::Cloud::PubSub.new# Publish a messagepublisher=pubsub.publisher"my-topic"message=publisher.publish"task completed"message.data#=> "task completed"# Listen for messagessubscriber=pubsub.subscriber"my-topic-sub"listener=subscriber.listendo|received_message|# process messagereceived_message.acknowledge!end# Start background threads that will call the block passed to listen.listener.start# Shut down the subscriber when ready to stop receiving messages.listener.stop!
Methods
#attributes
defattributes()
Optional attributes for the message.
#data
defdata()
The message payload. This data is a list of bytes encoded as
ASCII-8BIT.
#initialize
definitialize(data=nil,attributes={})->Message
Create an empty Message object.
This can be used to publish several messages in bulk.
The ID of this message, assigned by the server at publication time.
Guaranteed to be unique within the topic.
#ordering_key
defordering_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.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Cloud Pub/Sub API - Class Google::Cloud::PubSub::Message (v3.0.2)\n\nVersion latestkeyboard_arrow_down\n\n- [3.0.2 (latest)](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Message)\n- [3.0.1](/ruby/docs/reference/google-cloud-pubsub/3.0.1/Google-Cloud-PubSub-Message)\n- [2.23.0](/ruby/docs/reference/google-cloud-pubsub/2.23.0/Google-Cloud-PubSub-Message)\n- [2.22.0](/ruby/docs/reference/google-cloud-pubsub/2.22.0/Google-Cloud-PubSub-Message)\n- [2.21.0](/ruby/docs/reference/google-cloud-pubsub/2.21.0/Google-Cloud-PubSub-Message)\n- [2.20.0](/ruby/docs/reference/google-cloud-pubsub/2.20.0/Google-Cloud-PubSub-Message)\n- [2.19.0](/ruby/docs/reference/google-cloud-pubsub/2.19.0/Google-Cloud-PubSub-Message)\n- [2.18.1](/ruby/docs/reference/google-cloud-pubsub/2.18.1/Google-Cloud-PubSub-Message)\n- [2.17.0](/ruby/docs/reference/google-cloud-pubsub/2.17.0/Google-Cloud-PubSub-Message)\n- [2.16.0](/ruby/docs/reference/google-cloud-pubsub/2.16.0/Google-Cloud-PubSub-Message)\n- [2.15.5](/ruby/docs/reference/google-cloud-pubsub/2.15.5/Google-Cloud-PubSub-Message)\n- [2.14.0](/ruby/docs/reference/google-cloud-pubsub/2.14.0/Google-Cloud-PubSub-Message)\n- [2.12.1](/ruby/docs/reference/google-cloud-pubsub/2.12.1/Google-Cloud-PubSub-Message)\n- [2.11.0](/ruby/docs/reference/google-cloud-pubsub/2.11.0/Google-Cloud-PubSub-Message)\n- [2.10.0](/ruby/docs/reference/google-cloud-pubsub/2.10.0/Google-Cloud-PubSub-Message)\n- [2.9.2](/ruby/docs/reference/google-cloud-pubsub/2.9.2/Google-Cloud-PubSub-Message) \nReference documentation and code samples for the Cloud Pub/Sub API class Google::Cloud::PubSub::Message.\n\nMessage\n-------\n\nRepresents a Pub/Sub Message.\n\nMessage objects are created by [Publisher#publish](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_publish_instance_ \"Google::Cloud::PubSub::Publisher#publish (method)\"). [Subscriber#pull](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Subscriber#Google__Cloud__PubSub__Subscriber_pull_instance_ \"Google::Cloud::PubSub::Subscriber#pull (method)\")\nreturns an array of [ReceivedMessage](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage \"Google::Cloud::PubSub::ReceivedMessage (class)\") objects, each of which contains a\nMessage object. Each [ReceivedMessage](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-ReceivedMessage \"Google::Cloud::PubSub::ReceivedMessage (class)\") object can be acknowledged and/or\ndelayed. \n\nInherits\n--------\n\n- Object\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/pubsub\"\n\npubsub = Google::Cloud::PubSub.new\n\n# Publish a message\npublisher = pubsub.publisher \"my-topic\"\nmessage = publisher.publish \"task completed\"\nmessage.data #=\u003e \"task completed\"\n\n# Listen for messages\nsubscriber = pubsub.subscriber \"my-topic-sub\"\nlistener = subscriber.listen do |received_message|\n # process message\n received_message.acknowledge!\nend\n\n# Start background threads that will call the block passed to listen.\nlistener.start\n\n# Shut down the subscriber when ready to stop receiving messages.\nlistener.stop!\n```\n\nMethods\n-------\n\n### #attributes\n\n def attributes()\n\nOptional attributes for the message.\n\n### #data\n\n def data()\n\nThe message payload. This data is a list of bytes encoded as\nASCII-8BIT.\n\n### #initialize\n\n def initialize(data = nil, attributes = {}) -\u003e Message\n\nCreate an empty Message object.\nThis can be used to publish several messages in bulk. \n**Returns**\n\n- ([Message](./Google-Cloud-PubSub-Message)) --- a new instance of Message\n\n### #message_id\n\n def message_id()\n\n**Aliases**\n\n- [#msg_id](./Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_msg_id_instance_) \nThe ID of this message, assigned by the server at publication time.\nGuaranteed to be unique within the topic.\n\n### #msg_id\n\n def msg_id()\n\n**Alias Of** : [#message_id](./Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_message_id_instance_) \nThe ID of this message, assigned by the server at publication time.\nGuaranteed to be unique within the topic.\n\n### #ordering_key\n\n def ordering_key() -\u003e String\n\nIdentifies related messages for which publish order should be\nrespected.\n\n\nGoogle Cloud Pub/Sub ordering keys provide the ability to ensure\nrelated messages are sent to subscribers in the order in which they\nwere published. Messages can be tagged with an ordering key, a string\nthat identifies related messages for which publish order should be\nrespected. The service guarantees that, for a given ordering key and\npublisher, messages are sent to subscribers in the order in which they\nwere published. Ordering does not require sacrificing high throughput\nor scalability, as the service automatically distributes messages for\ndifferent ordering keys across subscribers.\n\n\u003cbr /\u003e\n\nSee [Publisher#publish_async](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Publisher#Google__Cloud__PubSub__Publisher_publish_async_instance_ \"Google::Cloud::PubSub::Publisher#publish_async (method)\") and [Subscriber#listen](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-Subscriber#Google__Cloud__PubSub__Subscriber_listen_instance_ \"Google::Cloud::PubSub::Subscriber#listen (method)\"). \n**Returns**\n\n- (String)\n\n### #publish_time\n\n def publish_time()\n\n**Alias Of** : [#published_at](./Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_published_at_instance_) \nThe time at which the message was published.\n\n### #published_at\n\n def published_at()\n\n**Aliases**\n\n- [#publish_time](./Google-Cloud-PubSub-Message#Google__Cloud__PubSub__Message_publish_time_instance_) \nThe time at which the message was published."]]