Method: AWS::SNS::Topic#publish

Defined in:
lib/aws/sns/topic.rb

#publish(default_message, options = {}) ⇒ String

Publishes a message to this SNS topic.

topic.publish('a short message')

You can pass a subject that is used when sending the message to email endpoints:

topic.publish('message', :subject => 'SNS message subject')

If you would like to pass a different message to various protocols (endpoint types) you can pass those as options:

topic.publish('default message',
  :http => "message sent to http endpoints",
  :https => "message sent to https endpoints",
  :email => "message sent to email endpoints")

The full list of acceptable protocols are listed below. The default message is sent to endpoints who’s protocol was not listed.

Parameters:

  • default_message (String)

    The message you want to send to the topic. Messages must be UTF-8 encoded strings at most 8 KB in size (8192 bytes, not 8192 characters).

  • Options (Hash)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :subject (String)

    Used as the “Subject” line when the message is delivered to email endpoints. Will also be included in the standard JSON messages delivered to other endpoints.

    • must be ASCII text that begins with a letter, number or punctuation mark

    • must not include line breaks or control characters

    • and must be less than 100 characters long

  • :http (String)
    • Message to use when sending to an

    HTTP endpoint.

  • :https (String)
    • Message to use when sending to an

    HTTPS endpoint.

  • :email (String)
    • Message to use when sending to an

    email endpoint.

  • :email_json (String)
    • Message to use when sending

    to an email json endpoint.

  • :sqs (String)
    • Message to use when sending to an

    SQS endpoint.

Returns:

  • (String)

    Returns the ID of the message that was sent.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/aws/sns/topic.rb', line 245

def publish default_message, options = {}

  message = { :default => default_message }

  [:http, :https, :email, :email_json, :sqs].each do |protocol|
    if options[protocol]
      message[protocol.to_s.gsub(/_/, '-')] = options[protocol]
    end
  end

  publish_opts = {}
  publish_opts[:message] = message.to_json
  publish_opts[:message_structure] = 'json'
  publish_opts[:subject] = options[:subject] if options[:subject]
  publish_opts[:topic_arn] = arn
  
  response = client.publish(publish_opts)

  response.message_id

end