Search code examples
rubyamazon-web-servicessms

Amazon SNS sending confirmation that SMS text message was sent with Ruby SDK


I'm sending SMS text messages using Amazon SNS with Ruby Amazon SDK. As far I understood, the method Aws::SNS::Client#publish just enqueues the message sending.

When a messageId is returned, the message has been saved and Amazon SNS will attempt to deliver it to the topic's subscribers shortly.

I'd to know if the publish method just enqueues the sending and if so, is there any way to confirm with the SDK that the message was successfully sent?


Solution

  • You can query for topics or for subscriptions, but you can't query for published messages. I doubt AWS keeps them, but if they do, there is no API to access them so far as I know.

    One workaround would be to subscribe to each of your own topics using an http or https endpoint on your own site. Each time you create a topic, you would create a corresponding subscription to your own endpoint:

    def generate_sns_topic(topic_name)
      sns_client = Aws::SNS::Client.new
      response = sns_client.create_topic(name: topic_name)
      if response.successful?
         sns_client.subscribe(topic_arn: topic_response.topic_arn, 
                              protocol: :https, 
                              endpoint: <your_site_endpoint>)
      else
        <error_handling_here>
      end
    end
    

    Now you need to create POST <your_site_endpoint> to accept the AWS message as it comes in. Once you receive it, you'll know SNS has sent it. Presumably you'll create a new published_messages table in your database to keep track of what's been published.