AWS SDK for Ruby
Developer Guide

Sending and Receiving Messages in Amazon SQS

After you create a queue in Amazon SQS, you can send a message to it and then consume it. To learn more, see Tutorial: Sending a Message to an Amazon SQS Queue and Tutorial: Receiving and Deleting a Message from an Amazon SQS Queue.

In this example, you use the AWS SDK for Ruby with Amazon SQS to:

  1. Send a message to a queue by using Aws::SQS::Client#send_message.

  2. Receive the message in the queue by using Aws::SQS::Client#receive_message.

  3. Display information about the message.

  4. Delete the message from the queue by using Aws::SQS::Client#delete_message.

Prerequisites

Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:

You also need to create the queue my-queue, which you can do in the Amazon SQS console.

Example

require 'aws-sdk-sqs' # v2: require 'aws-sdk' sqs = Aws::SQS::Client.new(region: 'us-east-1') # Send a message to a queue. queue_name = "my-queue" begin queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url # Create a message with three custom attributes: Title, Author, and WeeksOn. send_message_result = sqs.send_message({ queue_url: queue_url, message_body: "Information about current NY Times fiction bestseller for week of 2016-12-11.", message_attributes: { "Title" => { string_value: "The Whistler", data_type: "String" }, "Author" => { string_value: "John Grisham", data_type: "String" }, "WeeksOn" => { string_value: "6", data_type: "Number" } } }) rescue Aws::SQS::Errors::NonExistentQueue puts "A queue named '#{queue_name}' does not exist." exit(false) end puts send_message_result.message_id # Receive the message in the queue. receive_message_result = sqs.receive_message({ queue_url: queue_url, message_attribute_names: ["All"], # Receive all custom attributes. max_number_of_messages: 1, # Receive at most one message. wait_time_seconds: 0 # Do not wait to check for the message. }) # Display information about the message. # Display the message's body and each custom attribute value. receive_message_result.messages.each do |message| puts message.body puts "Title: #{message.message_attributes["Title"]["string_value"]}" puts "Author: #{message.message_attributes["Author"]["string_value"]}" puts "WeeksOn: #{message.message_attributes["WeeksOn"]["string_value"]}" # Delete the message from the queue. sqs.delete_message({ queue_url: queue_url, receipt_handle: message.receipt_handle }) end

On this page: