Introduction
Amazon SNS (Simple Notification Service) is a fully managed pub/sub messaging service which enables you to write distributed applications. Using SNS, you can manage and send notifications to all the subscribed system using endpoints like SQS and webhooks. It can also send messages to the Lambda function for further processing. SNS can also be used to maintain a large number of human subscribers too. People can get notifications using SMS and emails. In this part, we will see how can we publish a message using SNS.
Before we begin let us first understand what is Publisher/Subscriber model.
Pre-requisites
You will need a valid AWS account and credentials to access the SNS. You will also need to have access to the AWS console to create an Amazon SNS topic and some subscribers to it.
Setting up a SNS Topic
To set up the Amazon SNS topic you first log in to AWS and navigate to SNS. Follow the instruction to create a SNS and a topic. Once created, you will need the ARN property of the SNS to use in the code. Make sure, the credentials you are using have access to publish a message from the SNS. Add some subscribers and confirm them to see the full action.
Publishing a message
Let\’s assume the following is the message.
{ \"foo\": \"bar\"}
Now that we have a message structure, we need to publish it out to the desired SNS. We have to import the AWS SDK for node.js and use it to publish a message. The SDK is capable of using the credentials stored in your env. It looks for the following environment variable:-
export AWS_ACCESS_KEY_ID=your_access_key_idexport AWS_SECRET_ACCESS_KEY=your_secret_access_keyexport AWS_REGION=the_region_you_are_using
Following is the code to publish the message:-
// Getting Started with AWS SNS using node js. //This part shows how to publish content to SNS // Load the AWS SDK for Node.js const AWS = require(\"aws-sdk\"); const sns = new AWS.SNS({apiVersion: \"2010-03-31\"}); const params = { \"Message\": JSON.stringify({\"foo\": \"bar\"}), \"TopicArn\": \"ARN FOR TOPIC YOU WANT TO PUBLISH TO\" }; // By using Callback sns.publish(params, (err, data) => { if (err) { console.log(\"There was an Error: \", err); } else { console.log(\"Successfully published.\", data); }});
The above is implemented using the callback. if you wish to achieve the implementation using promise, the following is the implementation.
// Getting Started with AWS SNS using node js. //This part shows how to publish content to SNS // Load the AWS SDK for Node.js const AWS = require(\"aws-sdk\"); const sns = new AWS.SNS({apiVersion: \"2010-03-31\"}); const params = { \"Message\": JSON.stringify({\"foo\": \"bar\"}), \"TopicArn\": \"ARN FOR TOPIC YOU WANT TO PUBLISH TO\" }; // Promise implementation sns.publish(params).promise() .then(data => console.log(\"Successfully published.\", data)).catch(err => console.log(\"There was an Error: \", err));
You can also find the code sample in my GitHub repo
Conclusion
AWS Simple Notification Service (SNS) is a super scalable service that allows us to implement the publish/subscribe model with ease. We can use this to send texts, emails, push notifications, or other automated messages to multiple channels at the same time. There are many other uses cases and some advanced filtering logic, message templating and mixed message available in SNS. Give it a try and Happy Coding.