title: Nodejs sqs queue processor
body: <p>I am trying to write a nodejs sqs queue processor. </p>
  
  <pre><code>"use strict";
  var appConf = require('./config/appConf');
  var AWS = require('aws-sdk');
  AWS.config.loadFromPath('./config/aws_config.json');
  var sqs = new AWS.SQS();
  var exec = require('child_process').exec;
  function readMessage() {
    sqs.receiveMessage({
      "QueueUrl": appConf.sqs_distribution_url,
      "MaxNumberOfMessages": 1,
      "VisibilityTimeout": 30,
      "WaitTimeSeconds": 20
    }, function (err, data) {
      var sqs_message_body;
      if (data.Messages) {
        if (typeof data.Messages[0] !== 'undefined' &amp;&amp; typeof data.Messages[0].Body !== 'undefined') {
          //sqs msg body
          sqs_message_body = JSON.parse(data.Messages[0].Body);
          //make call to nodejs handler in codeigniter
          exec('php '+ appConf.CI_FC_PATH +'/index.php nodejs_handler make_contentq_call "'+ sqs_message_body.contentq_cat_id+'" "'+sqs_message_body.cnhq_cat_id+'" "'+sqs_message_body.network_id+'"',
            function (error, stdout, stderr) {
              if (error) {
                throw error;
              }
              console.log('stdout: ' + stdout);
              if(stdout == 'Success'){
                //delete message from queue
                sqs.deleteMessage({
                  "QueueUrl" : appConf.sqs_distribution_url,
                  "ReceiptHandle" :data.Messages[0].ReceiptHandle
                });
              }
            });
        }
      }
    });
  }
  readMessage();
  </code></pre>
  
  <p>The above code works fine for single message in queue. How should I write this script so that it keeps polling for messages in queue untill all messages are processed? Should i use set timeout?</p>