Created
March 28, 2021 06:42
-
-
Save kaushalvivek/e289a82f33033684267562b420fe21e5 to your computer and use it in GitHub Desktop.
A function to consume messages for AWS 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
const AWS = require('aws-sdk') | |
const https = require('https') | |
const sqsConfig = { | |
apiVersion: "2012-11-05", | |
accessKeyId: "<sqs-user's ACCESS_KEY_ID here>", | |
secretAccessKey: "<sqs-user's SECRET_ACCESS_KEY here>", | |
region: "AWS REGION HERE" // you can see your AWS region in your queue ARN, like: | |
// arn:aws:sqs:<AWS REGION>:user-id:queue-name | |
} | |
AWS.config.update(sqsConfig) | |
const createConsumer = function (queueUrl, batchSize, handler) { | |
return Consumer.create({ | |
queueUrl: queueUrl, | |
batchSize: batchSize, | |
handleMessageBatch: handler, | |
sqs: new AWS.SQS({ | |
httpOptions: { | |
agent: new https.Agent({ | |
keepAlive: true | |
}) | |
} | |
}) | |
}) | |
} | |
const doThisWithMessages(messages){ | |
// process messages in this function | |
console.log(messages); | |
}; | |
const sampleConsumer = createConsumer( | |
"https://<QUEUE_URL_HERE -- copy from SQS console>", // URL of the queue to consume | |
10, // batch size -- number of messages to consume at once, <=10 | |
doThisWithMessages // handler for messages | |
); | |
sampleConsumer.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment