Sending Messages in Amazon SQS - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Sending Messages in Amazon SQS

The following example sends the message “Hello world” through the Amazon SQS queue with the URL URL in the us-west-2 region.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-sqs' require 'aws-sdk-sts' # Sends a message to a queue in Amazon Simple Queue Service (Amazon SQS). # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param message_body [String] The contents of the message to be sent. # @return [Boolean] true if the message was sent; otherwise, false. # @example # exit 1 unless message_sent?( # Aws::SQS::Client.new(region: 'us-east-1'), # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue', # 'This is my message.' # ) def message_sent?(sqs_client, queue_url, message_body) sqs_client.send_message( queue_url: queue_url, message_body: message_body ) true rescue StandardError => e puts "Error sending message: #{e.message}" false end # Full example call: def run_me region = 'us-east-1' queue_name = 'my-queue' message_body = 'This is my message.' sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue' queue_url = 'https://sqs.' + region + '.amazonaws.com/' + sts_client.get_caller_identity.account + '/' + queue_name sqs_client = Aws::SQS::Client.new(region: region) puts "Sending a message to the queue named '#{queue_name}'..." if message_sent?(sqs_client, queue_url, message_body) puts 'Message sent.' else puts 'Message not sent.' end end run_me if $PROGRAM_NAME == __FILE__

The following example sends the messages “Hello world” and “How is the weather?” through the Amazon SQS queue with the URL URL in the us-west-2 region.

Note

If your queue is a FIFO queue, you must include a message_group_id parameter in addition to the id and message_body parameters.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 require 'aws-sdk-sqs' require 'aws-sdk-sts' # Sends multiple messages as a batch to a queue in # Amazon Simple Queue Service (Amazon SQS). # # @param sqs_client [Aws::SQS::Client] An initialized Amazon SQS client. # @param queue_url [String] The URL of the queue. # @param entries [Hash] The contents of the messages to be sent, # in the correct format. # @return [Boolean] true if the messages were sent; otherwise, false. # @example # exit 1 unless messages_sent?( # Aws::SQS::Client.new(region: 'us-east-1'), # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue', # [ # { # id: 'Message1', # message_body: 'This is the first message.' # }, # { # id: 'Message2', # message_body: 'This is the second message.' # } # ] # ) def messages_sent?(sqs_client, queue_url, entries) sqs_client.send_message_batch( queue_url: queue_url, entries: entries ) true rescue StandardError => e puts "Error sending messages: #{e.message}" false end # Full example call: def run_me region = 'us-east-1' queue_name = 'my-queue' entries = [ { id: 'Message1', message_body: 'This is the first message.' }, { id: 'Message2', message_body: 'This is the second message.' } ] sts_client = Aws::STS::Client.new(region: region) # For example: # 'https://sqs.us-east-1.amazonaws.com/111111111111/my-queue' queue_url = 'https://sqs.' + region + '.amazonaws.com/' + sts_client.get_caller_identity.account + '/' + queue_name sqs_client = Aws::SQS::Client.new(region: region) puts "Sending messages to the queue named '#{queue_name}'..." if messages_sent?(sqs_client, queue_url, entries) puts 'Messages sent.' else puts 'Messages not sent.' end end run_me if $PROGRAM_NAME == __FILE__