Laerion
Laerion

Reputation: 865

Most effective way to poll an Amazon SQS queue using Node

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!

Upvotes: 20

Views: 17454

Answers (3)

nickool
nickool

Reputation: 844

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

Upvotes: 29

ketan vijayvargiya
ketan vijayvargiya

Reputation: 5649

As mentioned by Mircea, long polling is one option.

When you ask for a 'trigger', I believe you are looking for something other than continuously polling SQS on your own. If that is the case, I'd suggest you look at AWS Lambda. It allows you to put code in the cloud, which automatically gets triggered on your configured events, such as SNS event, a file pushed to S3 etc.

http://aws.amazon.com/lambda/

Upvotes: 0

Mircea
Mircea

Reputation: 10566

yes there is: http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html

you can configure the SQS queues to have a "receive message wait time" and do long polling.

so you can set it to say 10 seconds, and the call will come back only if you have a message or after the 10 sec timeout expires. you can continuously poll the queue in this scenario.

Upvotes: 3

Related Questions