Last active
December 27, 2018 10:30
-
-
Save carlozamagni/ece0ebb0fa7db5711aa45f460ee01c82 to your computer and use it in GitHub Desktop.
Receive and Publish operations via AWS SNS using Nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PUBLISH TO AWS SNS // | |
const AWS = require('aws-sdk') | |
// external config file | |
// const config = require('./configuration.js') | |
const config = {'SNS_ACCESS_KEY_ID':'', 'SNS_SECRET_KEY_ID':''} | |
const topicArn = '' | |
// perform auth | |
AWS.config.update({ | |
accessKeyId: config.SNS_ACCESS_KEY_ID, | |
secretAccessKey: config.SNS_SECRET_KEY_ID, | |
region: 'us-east-1' | |
}); | |
var sns = new AWS.SNS(); | |
// publish | |
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#publish-property | |
sns.publish({ | |
TopicArn: topicArn, | |
Message:'Simple text message', | |
Subject: "just testing" | |
}, function(err, data){ | |
if (err){ | |
console.log("Error sending a message "+err) | |
}else{ | |
console.log("Sent message {0}".format(data.MessageId)) | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PUBLISH TO AWS SNS // | |
const AWS = require('aws-sdk') | |
// perform auth | |
AWS.config.update({ | |
accessKeyId: '', | |
secretAccessKey: '', | |
region: '' | |
}) | |
var sns = new AWS.SNS() | |
// publish | |
// http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#publish-property | |
async function PublishMessage(message, subject) { | |
let m = (typeof message == 'string' || message instanceof String) ? message : JSON.stringify(message) | |
const msg = { | |
TopicArn: '', | |
MessageStructure: 'json', | |
Message: JSON.stringify({ | |
'http': m, | |
'https': m, | |
'email': m, | |
'default': m | |
}), | |
Subject: subject | |
} | |
await sns.publish(msg, subject).promise() | |
} | |
PublishMessage({ 'sub': 'test', 'mun': 1 }).catch(e => console.error(e)).then(() => { console.log('message sent.') }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http') | |
const port = 8080 | |
const requestHandler = (request, response) => { | |
response.statusCode = 200 | |
var messageType = request.headers['x-amz-sns-message-type'] | |
var body = ''; | |
request.on('data', function (data) { | |
body += data; | |
}).on('end', function () { | |
console.log(JSON.parse(body)) | |
//body = JSON.parse(body) | |
if(messageType === 'Notification'){ | |
console.log('... message processing') | |
}else if(messageType === 'SubscriptionConfirmation'){ | |
console.log('... subscription confirmation') | |
console.log('HTTP GET to url: ' + body.SubscribeURL) | |
} | |
response.end('Done!') | |
}) | |
} | |
const server = http.createServer(requestHandler) | |
server.listen(port, (err) => { | |
if (err) { | |
return console.log('server is down: ', err) | |
} | |
console.log(`server is listening on ${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment