Rougher
Rougher

Reputation: 848

SQS receiveMessage stops to work after first message

I'm new in SQS and nodejs. I want to receive messages while queue isn't empty, but after receiving first message it stops to work. My code reads first message only in starting of server. How can I read messages all time?

sqs.receiveMessage({
    QueueUrl: queueUri,
    WaitTimeSeconds: 3,
    MaxNumberOfMessages: 1
 }, function(err, data) {
   // If there are any messages to get
   if (data.Messages) {
      // Get the first message (should be the only one since we said to only get one above)
      var message = data.Messages[0],
          body = JSON.parse(message.Body);
      var bucketName = body.Records[0].s3.bucket.name;
      var fullName = body.Records[0].s3.object.key;
      var fileName = Path.basename(fullName);
      var folderName = Path.dirname(fullName);
      var _45px = {
        width: 45,
        dstnKey: fileName,
        destinationPath: "thumbnails"
      };

      removeFromQueue(message);
 }
 });

Upvotes: 1

Views: 896

Answers (1)

Mark B
Mark B

Reputation: 200988

That code will read one message and stop. If you want to continue reading messages you need to perform some sort of loop to continue calling sqs.receiveMessage(). Of course that can be difficult since it is an asynchronous call. I would look into the Async library.

Upvotes: 1

Related Questions