Search code examples
ruby-on-railsrubyamazon-web-servicesamazon-sqs

Using AWS SQS with Ruby on Rails


I am unable to find any example on how to Use Amazon AWS SQS service in conjunction with Ruby on Rails.

Please could someone provide a simple, barebones example of using SQS to send email.


Solution

  • You may look into these examples. I have taken this from the GitHub GIST - AWS SQS Example.

    #!/usr/bin/env ruby
    
    require 'yaml'
    require 'aws-sdk'
    
    config_file = File.join(File.dirname(__FILE__),"config.yml")
    config = YAML.load(File.read(config_file))
    AWS.config(config)
    
    # http://rubydoc.info/github/amazonwebservices/aws-sdk-for-ruby/master/AWS/SQS
    
    sqs = AWS::SQS.new
    queue = sqs.queues.create("my_queue")
    
    # http://rubydoc.info/github/amazonwebservices/aws-sdk-for-ruby/master/AWS/SQS/Queue
    
    send = lambda { |name, queue|
      while true do
        queue.send_message("#{name}:#{Time.now.to_s}")
        sleep 1
      end
    }
    
    Thread.new { send.call("t1", queue) }
    Thread.new { send.call("t2", queue) }
    Thread.new { send.call("t3", queue) }
    
    sleep 1000