Skip to content

Instantly share code, notes, and snippets.

@alexanderjamesking
Last active March 26, 2017 09:13
Show Gist options
  • Save alexanderjamesking/d7d852db0188f64965da to your computer and use it in GitHub Desktop.
Save alexanderjamesking/d7d852db0188f64965da to your computer and use it in GitHub Desktop.
Node AWS - SNS and SQS
// 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
});
// 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
}
);
}
});
// 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