Last active
March 26, 2017 09:13
-
-
Save alexanderjamesking/d7d852db0188f64965da to your computer and use it in GitHub Desktop.
Node AWS - SNS and SQS
This file contains 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 a message to an SNS topic | |
var AWS = require('aws-sdk'); | |
AWS.config.loadFromPath('./config.json'); | |
var sns = new AWS.SNS(); | |
var params = { | |
TopicArn : "arn:your-topic-arn", | |
Message: "Hello World" | |
}; | |
sns.publish(params, function(err, data) { | |
// published message | |
}); |
This file contains 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
// consume a message from an SQS queue | |
var AWS = require('aws-sdk'); | |
AWS.config.loadFromPath('./config.json'); | |
var sqs = new AWS.SQS(); | |
var queueUrl = "https://your-queue-url"; | |
sqs.receiveMessage({ | |
QueueUrl : queueUrl | |
}, | |
function (err, data) { | |
if (data.Messages && data.Messages.length > 0) { | |
var message = data.Messages[0]; | |
// TODO: process the message... | |
// Delete the message from the queue | |
var receiptHandle = message.ReceiptHandle; | |
sqs.deleteMessage({ | |
QueueUrl: queueUrl, | |
ReceiptHandle: receiptHandle | |
}, | |
function (err, data) { | |
// message deleted | |
} | |
); | |
} | |
}); |
This file contains 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
// config.json to store AWS credentials for this example | |
{ | |
"accessKeyId": "YOUR_ACCESS_KEY", | |
"secretAccessKey": "YOUR_SECRET_ACCESS_KEY", | |
"region": "eu-west-1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment