How can I get the nodejs server to listen on AWS SQS?

Before I explain the problem in detail, I will tell you about my current approach.

I have js script that run setInterval (). and every interval I will call SQS to get a message from the queue. If there is a message, I process it.
therefore it will work endlessly until I kill the process.

I also created the node server before (using the example in nodejs.org)

So, I am wondering, instead of running setInterval periodically. Is there a way that if a new message appears in SQS, it will fire an event and process the message?

+8
events amazon-sqs
source share
4 answers

These questions are more than 2 years old .. but there is a much better way than changing your survey interval. Instead, set the waiting time for a message in the queue to 20 seconds. Then you can perform continuous polling, but you will only make 3 requests per minute while the queue is empty. When there is data in the queue, the response will be immediate.

+7
source share

Not. You must request a message from SQS.

Check out SNS if you really need push notifications. SNS works well if you want to give your server a hint to poll SQS after adding a message to the queue.

+6
source share

SQS does not provide notification, but if there is a new message in SQS, can you create everything that created the message, click node.js and enable polling for several minutes. You may not be able to control what is in your queue, but if so, I would ask him to run node.js to start polling the queue.

If you are concerned about the survey because of the cost, you can do what I did - dynamically change the time of the survey. SQS queues are checked every 5 seconds. If node detects a message, it immediately increases the polling time to 200 ms in a few seconds. If it does not find a message in the queue, it slows down 50 ms every empty request until the 5 second polling is deleted again.

This first request will be slow, which you may not be able to handle. To combat this, my polling time is randomly accelerated every few minutes. When polling multiple nodes, the response time is usually very fast.

+3
source share

You can use SQS Long Polling to achieve this. Long polling reduces the number of blank responses, allowing Amazon SQS to wait until a message appears in the queue before sending a response. It will also significantly reduce SQS costs.

To do this, there is an excellent library called sqs-consumer . This allows you to simply define the function that receives the SQS message and call the 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, the long polling method described above is already being used.

+2
source share

All Articles