Messages Through Amazon SNS in NodeJs
Posted By : Arun Kataria | 24-Oct-2018
Amazon Simple Notification Service (SNS) is an exceptionally accessible, secure, durable completely oversaw pub/sub informing service that empowers you to decouple microservices, serverless applications, and appropriated frameworks. Amazon SNS gives themes to high-throughput, many-to-many informing, and push-based. Utilizing Amazon SNS points, your distributor frameworks can fan out messages to countless endpoints for parallel handling, including Amazon SQS queues, AWS Lambda functions, and HTTP/S webhooks. Moreover, SNS can be utilized to fan out warnings to end clients utilizing portable push, SMS, and email.
You can begin with Amazon SNS in minutes by utilizing the AWS CLI (Command Line Interface), AWS Management Console, or AWS SDK (Software Development Kit).
Amazon has stuffed a case brimming with administrations and shrouded almost every angle in making cloud a smooth ordeal for designers to set up exceedingly operational and basically complex applications on AWS. Huge quantities of cloud-based frameworks feel the critical need of or reliance on conveying messages on a membership premise to end clients. Yet, tragically that the web today is overflowing with spammers who decline to quit sending solicitations and impeded messages from servers with shocking SPF arrangement.
let me explain the figure:
1) Topics: These are the passageways or named gatherings of occasions for each distinguishing a particular subject, substance or occasion compose. Each and every point conveys along a Unique Identifier (UI) that aides in recognizing the SNS endpoint for distributing and buying in.
2) Owners: Owners are the individuals who make themes and have an entire induction and access to the point.
3) Subscribers: These are the customers, for the most part, the end-clients, servers, applications or different gadgets that craving to get notices on specific points of their advantage.
Publishers: They are in charge of sending messages to subjects. Presently SNS coordinates the point with the rundown of endorsers inspired by the subject and disperses the message to every single one of them.
In Node.Js we use "
NPM command to install:
npm i aws-sdk
NodeJs code for AWS SNS service.
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'REGION'});
// Create publish parameters
var params = {
Message: 'MESSAGE_TEXT', /* required */
TopicArn: 'TOPIC_ARN'
};
// Create promise and SNS service object
var publishTextPromise = new AWS.SNS({apiVersion: '2010-03-31'}).publish(params).promise();
// Handle promise's fulfilled/rejected states
publishTextPromise.then(
function(data) {
console.log(`Message ${params.Message} send sent to the topic ${params.TopicArn}`);
console.log("MessageID is " + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
Above code is used to send the messages to the user through SNS.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Arun Kataria
Arun has good skills in AngularJS, NodeJS, MongoDB and many more. He is highly motivated which allow him to strive for proficiency when accomplishing assigned duties. He is an excellent team operative.