[[["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-08-28 UTC."],[],[],null,["# Cloud Pub/Sub API - Class Google::Cloud::PubSub::BatchPublisher (v3.0.2)\n\nVersion latestkeyboard_arrow_down\n\n- [3.0.2 (latest)](/ruby/docs/reference/google-cloud-pubsub/latest/Google-Cloud-PubSub-BatchPublisher)\n- [3.0.1](/ruby/docs/reference/google-cloud-pubsub/3.0.1/Google-Cloud-PubSub-BatchPublisher)\n- [2.23.0](/ruby/docs/reference/google-cloud-pubsub/2.23.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.22.0](/ruby/docs/reference/google-cloud-pubsub/2.22.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.21.0](/ruby/docs/reference/google-cloud-pubsub/2.21.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.20.0](/ruby/docs/reference/google-cloud-pubsub/2.20.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.19.0](/ruby/docs/reference/google-cloud-pubsub/2.19.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.18.1](/ruby/docs/reference/google-cloud-pubsub/2.18.1/Google-Cloud-PubSub-BatchPublisher)\n- [2.17.0](/ruby/docs/reference/google-cloud-pubsub/2.17.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.16.0](/ruby/docs/reference/google-cloud-pubsub/2.16.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.15.5](/ruby/docs/reference/google-cloud-pubsub/2.15.5/Google-Cloud-PubSub-BatchPublisher)\n- [2.14.0](/ruby/docs/reference/google-cloud-pubsub/2.14.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.12.1](/ruby/docs/reference/google-cloud-pubsub/2.12.1/Google-Cloud-PubSub-BatchPublisher)\n- [2.11.0](/ruby/docs/reference/google-cloud-pubsub/2.11.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.10.0](/ruby/docs/reference/google-cloud-pubsub/2.10.0/Google-Cloud-PubSub-BatchPublisher)\n- [2.9.2](/ruby/docs/reference/google-cloud-pubsub/2.9.2/Google-Cloud-PubSub-BatchPublisher) \nReference documentation and code samples for the Cloud Pub/Sub API class Google::Cloud::PubSub::BatchPublisher.\n\nTopic Batch Publisher object used to publish multiple messages at\nonce. \n\nInherits\n--------\n\n- Object\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/pubsub\"\n\npubsub = Google::Cloud::PubSub.new\n\npublisher = pubsub.publisher \"my-topic\"\nmsgs = publisher.publish do |batch_publisher|\n batch_publisher.publish \"task 1 completed\", foo: :bar\n batch_publisher.publish \"task 2 completed\", foo: :baz\n batch_publisher.publish \"task 3 completed\", foo: :bif\nend\n```\n\nMethods\n-------\n\n### #publish\n\n def publish(data, attributes = nil, ordering_key: nil, **extra_attrs)\n\nAdd a message to the batch to be published to the topic.\nAll messages added to the batch will be published at once.\nSee Topic#publish \n**Parameters**\n\n- **data** (String, File) --- The message payload. This will be converted to bytes encoded as ASCII-8BIT.\n- **attributes** (Hash) --- Optional attributes for the message.\n- **ordering_key** (String) *(defaults to: nil)* --- Identifies related messages for which publish order should be respected.\n**Example**\n\nMultiple messages can be sent at the same time using a block: \n\n```ruby\nrequire \"google/cloud/pubsub\"\n\npubsub = Google::Cloud::PubSub.new\n\npublisher = pubsub.publisher \"my-topic\"\nmsgs = publisher.publish do |batch_publisher|\n batch_publisher.publish \"task 1 completed\", foo: :bar\n batch_publisher.publish \"task 2 completed\", foo: :baz\n batch_publisher.publish \"task 3 completed\", foo: :bif\nend\n```"]]