Skip to content

Instantly share code, notes, and snippets.

@damiancipolat
Last active October 17, 2020 05:53
Show Gist options
  • Save damiancipolat/a16e0d218633d4c1a8835dcf7416be99 to your computer and use it in GitHub Desktop.
Save damiancipolat/a16e0d218633d4c1a8835dcf7416be99 to your computer and use it in GitHub Desktop.
A node.js example using the aws-sdk to consumer data
const AWS = require('aws-sdk');
AWS.config.update({
region: 'us-east-1'
});
const SQS = new AWS.SQS();
const {
Consumer
} = require('sqs-consumer');
const URL = 'https://sqs.us-east-1.amazonaws.com/ssssssssssss/kyc-event-sqs';
const deleteMessage = (url, receiptHandle)=>{
const deleteParams = {
QueueUrl: url,
ReceiptHandle: receiptHandle
};
return SQS.deleteMessage(deleteParams).promise();
}
const onMessage = async (msg)=>{
const {
MessageId,
ReceiptHandle,
Body
} = msg;
console.log('Received:',Body,ReceiptHandle,MessageId);
await deleteMessage(URL,ReceiptHandle);
};
const onError = (error)=>{
console.error('error:',error);
};
const app = Consumer.create({
queueUrl: URL,
handleMessage: onMessage,
batchSize:1,
attributeNames:['All'],
messageAttributeNames:['All'],
sqs: new AWS.SQS()
});
app.on('error', onError);
app.on('processing_error', onError);
app.on('empty', ()=>console.log('empty'));
app.start();
console.log('Waiting messages from queue...');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment