Amazon Web Services SNS support for Node.js

Supported APIs

The following APIs are supported:

For SDK V2

AWS.SNS({apiVersion: '2010-03-31'}).publish(params)
NodeJS AWS SNS Publisher object is created. Its name is that of the topic.
AWS.SNS({apiVersion: '2010-03-31'}).subscribe(params)
NodeJS AWS SNS Subscriber object is created. Its name is that of the topic. Then for each supported protocol, an object is created with a callLink from the subscriber to that object.

For SDK V3

new SNSClient({region: REGION}).send(new PublishCommand(params));
NodeJS AWS SNS Publisher object is created. Its name is that of the topic.
new SNSClient({region: REGION}).send(new SubscribeCommand(params));
NodeJS AWS SNS Subscriber object is created. Its name is that of the topic. Then for each supported protocol, an object is created with a callLink from the subscriber to that object. 

Supported protocols

The com.castsoftware.wbslinker will create a callLink between the SNS Publishers and SNS Subscribers which have the same name.

Protocol Object created Object name
email NodeJS Email an Email (the email addresses are not evaluated).
sms NodeJS SMS an SMS (the SMS numbers are not evaluated).
http/https NodeJS AWS Post HttpRequers service the URL (evaluated from the endpoint).
sqs NodeJS AWS Simple Queue Service Publisher the name of the queue (evaluated from the endpoint).
lambda NodeJS Call to AWS Lambda Function the name of the Lambda function (evaluated from the endpoint).

Examples

API v2

When analyzing the following source code:

var AWS = require('aws-sdk');
// Set region
AWS.config.update({region: 'REGION'});
// Create promise and SNS service object

var sns = new AWS.SNS({apiVersion: '2010-03-31'})

function my_subscribe(params) {
    sns.subscribe(params, function (err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);           // successful response
    });
}

function my_publish(params) {
    sns.publish(params);
}

function foo() {
    let topicArn = "arn:aws:sns:eu-west-3:757025016730:testTopic";
    my_subscribe({Protocol: "EMAIL", TopicArn: topicArn, Endpoint: "EMAIL_ADDRESS"})
    my_subscribe({Protocol: "SMS", TopicArn: topicArn, Endpoint: "911"})
    my_subscribe({Protocol: "LAMBDA", TopicArn: topicArn, Endpoint: "arn:aws:lambda:eu-west-3:757025016730:testLambda"})
    my_subscribe({Protocol: "HTTP", TopicArn: topicArn, Endpoint: "http:/myapi.api.test.com"})
 }
function bar() {
    const params2 = {
        TopicArn: "arn:aws:sns:eu-west-3:757025016730:testTopic",
        Message: "MESSAGE_TEXT"
    };
    my_publish(params2)
}

The following results will be produced:

API v3

When analyzing the following source code:

const {SNSClient} = require("@aws-sdk/client-sns");
// Set the AWS Region.
const REGION = "us-east-1";
// Create SNS service object.
const snsClient = new SNSClient({region: REGION});
export {snsClient};

// ====== Import required AWS SDK clients and commands for Node.js
const {PublishCommand} = require("@aws-sdk/client-sns");
const {SubscribeCommand} = require("@aws-sdk/client-sns");

const subscription = async () => {
    try {
        let topicArn = "arn:aws:sns:eu-west-3:757025016730:testTopic";
        const data1 = await snsClient.send(new SubscribeCommand({Protocol: "EMAIL", TopicArn: topicArn, Endpoint: "test@mail.com"}));
        const data2 = await snsClient.send(new SubscribeCommand({Protocol: "SMS", TopicArn: topicArn, Endpoint: "911"}));
        const data3 = await snsClient.send(new SubscribeCommand({Protocol: "LAMBDA", TopicArn: topicArn, Endpoint: "arn:aws:lambda:eu-west-3:757025016730:testLambda"}));
        const data4 = await snsClient.send(new SubscribeCommand({Protocol: "HTTP", TopicArn: topicArn, Endpoint: "http:/myapi.api.test.com"}));
        console.log("Success.", data1);
        return data1;
    } catch (err) {
        console.log("Error", err.stack);
    }
};
subscription();

const run = async () => {
    // Set the parameters
    const params = {
        Message: "MESSAGE_TEXT", // MESSAGE_TEXT
        TopicArn: "arn:aws:sns:eu-west-3:757025016730:testTopic", //TOPIC_ARN
    };
    try {
        const data = await snsClient.send(new PublishCommand(params));
        console.log("Success.", data);
        return data; // For unit tests.
    } catch (err) {
        console.log("Error", err.stack);
    }
};
run();

The following results will be produced: