开发者

How can I make the nodejs server listen to AWS SQS?

before I explain the problem in details, I tell you my current approach.

I have a js script that run setInterval(). and each interval, I will call SQS to get the message from queue. If there is a message, then I process it.

so, it will run infinitely until I kill the process.

I have also built a node server before (using the example in the nodejs.org )

So, what I'm wondering is,.. instead of having the setInterval to run periodically. Is there a way that if There is a new messa开发者_如何学运维ge in the SQS, then it will fire an event and process the message?


This questions is over 2 years old.. but there is a much better way than changing your polling interval. Instead set the Receive Message Wait Time of your queue to the maximum of 20 seconds. Then you can do continuous polling but will only make 3 requests per minute while the queue is empty. When there is data in the queue the response will be immediate.


No. You must request a message from SQS.

Take a look at SNS if you really need push notifications. SNS works well if you want give your server a hint to poll SQS after you add a message to the queue.


You can use SQS Long Polling to achieve this. Long polling reduces the number of empty responses by allowing Amazon SQS to wait until a message is available in the queue before sending a response. This will also significantly reduce the costs for SQS.

To make this easy there is an excellent library called sqs-consumer. It allows you to just define a function that receives an SQS message and call a callback when the message has been processed:

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

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

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

app.start();

Under the hood it already makes use of the long polling technique described above.


SQS doesn't provide for notification, but if there is a new message in the SQS, could you have whatever created the message also hit node.js and turn polling on for a few minutes. You may not be in control of what goes in your queue, but if you are, I would have it also trigger your node.js to start polling the queue.

If you're concerned about polling because of cost, you can do I what I did- Dynamically change your polling time. My SQS queues are being polled every 5 seconds. If a node detects a message, it immediately ramps the polling time down to 200ms for a few seconds. If it doesn't detect a message in the queue, it slows down 50ms every empty request, until the 5 second poll is hit again.

That first request is going to be slow, something you may not be able to deal with. To combat that, I have the polling times randomly speed up every few minutes. With a few nodes polling, the response times are usually very quick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜