dnltsk
dnltsk

Reputation: 1077

receive all SQS messages via NodeJS client

I am confused about the behaviour of the SQS Client in Node. It seems not possible to loop through all messages of a queue having a single SQS instance in place. The code below returns zero messages when loaded within the loop.

const sqs = new SQS();    
let messages = await sqs.receiveMessage({ QueueUrl: "...", MaxNumberOfMessages: 10 }).promise();
while (messages?.Messages?.length > 0) {
  messages.Messages.forEach((message) => { console.log(message.Body); });
  messages = await sqs.receiveMessage({ QueueUrl: "...", MaxNumberOfMessages: 10 }).promise();
}

Once I changed the code to use a new instance of SQS inside the loop each time, as listed below, it works fine.

let messages = await new SQS().receiveMessage({ QueueUrl: "...", MaxNumberOfMessages: 10 }).promise();
while (messages?.Messages?.length > 0) {
  messages.Messages.forEach((message) => { console.log(message.Body); });
  messages = await new SQS().receiveMessage({ QueueUrl: "...", MaxNumberOfMessages: 10 }).promise();
}

Unfortunately, I wasn't able to find any hint about that in the documentation.

Does someone have a clue what causes that behaviour?

EDIT: added await keyword into the lines where .promise() is used. Thx to Mark B

Upvotes: 1

Views: 550

Answers (1)

dnltsk
dnltsk

Reputation: 1077

Figured out that the reason of the described behaviour was the fifo-configuration of the Queue.

Removing the fifo: true configuration solved my issue.

Upvotes: 1

Related Questions