Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass an object to amazon SNS

I see in the examples how to to pass a message string to amazon sns sdk's publish method. However, is there an exmaple of how to pass a custom object as the message? I tried setting "MessageStructure" to "json" but then I get InvalidParameter: Invalid parameter: Message Structure - No default entry in JSON message body error. Where should I be passing the object values into in the params?

Any examples?

    var params = {
        Message: JSON.stringify(item),
        MessageStructure: 'json',
        TopicArn: topic
        //MessageAttributes: item
    };

    return sns.publishAsync(params);
like image 373
MonkeyBonkey Avatar asked Jul 14 '15 21:07

MonkeyBonkey


People also ask

How do I push a message to AWS SNS?

To publish messages to Amazon SNS topics using the AWS Management Console. Sign in to the Amazon SNS console . In the left navigation pane, choose Topics. On the Topics page, select a topic, and then choose Publish message.

How do I send messages to SNS?

To send a message In the Amazon SNS console , on the Topics page, choose the name of the topic to which you want to send SMS messages. On the topic details page, choose Publish message.

Can SNS send JSON?

When Amazon SNS sends a notification to a subscribed HTTP or HTTPS endpoint, the POST message sent to the endpoint has a message body that contains a JSON document with the following name-value pairs. The Message value specified when the notification was published to the topic.

Can AWS SNS receive messages?

You can use Amazon Simple Notification Service (Amazon SNS) to send and receive Short Message Service (SMS) notifications to SMS-enabled mobile phones and smart phones. SMS notifications are currently supported for phone numbers in the United States. SMS messages can be sent only from topics created in the US East (N.


2 Answers

An example is shown here: https://docs.aws.amazon.com/sns/latest/api/API_Publish.html#API_Publish_Example_2

The JSON format for Message is as follows:

{
    "default": "A message.",
    "email": "A message for email.",
    "email-json": "A message for email (JSON).",
    "http": "A message for HTTP.",
    "https": "A message for HTTPS.",
    "sqs": "A message for Amazon SQS." 
} 

So, assuming what you wanted to pass is an object, the way it worked for me was:

const messageObjToSend = {
    ...
}

const params = {
    Message: JSON.stringify({
        default: JSON.stringify( messageObjToSend )
    }),
    MessageStructure: 'json',
    TopicArn: 'arn:aws:sns...'
}
like image 92
divillysausages Avatar answered Oct 09 '22 08:10

divillysausages


There is no SDK-supported way to pass a custom object as a message-- messages are always strings. You can, of course, make the string a serialized version of your object.

MessageStructure: 'json' is for a different purpose-- when you want to pass different strings to different subscription types. In that case, you make the message a serialized json object with AWS-defined structure, where each element defines the message to send to a particular type of subscription (email, sqs, etc). Even in that case, the messages themselves are just strings.

MessageAttributes are parameters you add to the message to support specific subscription types. If you are using SNS to talk to Apple's IOS notification service, for example, you might have to supply additional message parameters or authentication keys-- MessageAttributes provide a mechanism to do this. This is described in this AWS documentation.

like image 30
antlersoft Avatar answered Oct 09 '22 08:10

antlersoft