var AWS = require('aws-sdk'); var util = require('util'); var config = require('./config.json'); require('dotenv').config({ 'path': `${__dirname }/.env` }); // configure AWS AWS.config.update({ 'accessKeyId': process.env.AWS_ACCESS_KEY_ID, 'secretAccessKey': process.env.AWS_SECRET_ACCESS_KEY, 'region': process.env.AWS_REGION || 'eu-west-1' }); var sqs = new AWS.SQS(); var receiveMessageParams = { 'QueueUrl': config.QueueUrl, 'MaxNumberOfMessages': 10 }; function getMessages() { sqs.receiveMessage(receiveMessageParams, receiveMessageCallback); } function receiveMessageCallback(err, data) { // console.log(data); if (data && data.Messages && data.Messages.length > 0) { for (var i = 0; i < data.Messages.length; i++) { const body =JSON.parse( data.Messages[i].Body); if (body && body.Message) { const msg = JSON.parse(body.Message); console.dir(msg); } // console.dir(body); process.stdout.write('.'); // console.log("do something with the message here..."); // // Delete the message when we've successfully processed it const deleteMessageParams = { 'QueueUrl': config.QueueUrl, 'ReceiptHandle': data.Messages[i].ReceiptHandle }; // sqs.deleteMessage(deleteMessageParams, deleteMessageCallback); } getMessages(); } else { process.stdout.write('-'); setTimeout(getMessages, 1000); } } function deleteMessageCallback(err, data) { // console.log("deleted message"); // console.log(data); } setTimeout(getMessages, 1000);