Node.js – Most effective way to poll an Amazon SQS queue using Node

amazon-sqsamazon-web-servicesnode.js

My question is short, but I think is interesting:

I've a queue from Amazon SQS service, and I'm polling the queue every second. When there's a message I process the message and after processing, go back to polling the queue.

Is there a better way for this?, some sort of trigger? or which approach will be the best in your opinion, and why.

Thanks!

Best Answer

A useful and easily to use library for consuming messages from SQS is sqs-consumer

const Consumer = require('sqs-consumer');

const app = Consumer.create({
  queueUrl: 'https://sqs.eu-west-1.amazonaws.com/account-id/queue-name',
  handleMessage: (message, done) => {
    console.log('Processing message: ', message);
    done();
  }
});

app.on('error', (err) => {
  console.log(err.message);
});

app.start();

It's well documented if you need more information. You can find the docs at: https://github.com/bbc/sqs-consumer