Run callback when SQS gets message?

0

I'm making a NodeJS app with the aws-sdk package and SQS. My app needs to run some docker-related commands programmatically on my servers (it's a code running app), and I'm using SQS to slow the requests down. Right now, I am polling SQS at an interval of ~1 second for requests, but I think that this is a bad practice since it is steadily draining my limit.

Is there any API I can use to call a callback when SQS gets a message? Something like:

sqs.setCallback((err, data) => {
   //my code here
}, "onmessagerecieve")

If SQS doesn't have this API, are there any alternatives? Messages should be received in the queue in 1-30 seconds.

已提问 2 年前565 查看次数
1 回答
2
已接受的回答

There is no callback method for SQS as its a managed external queue.

You should take a look at enabling long polling on your queue and then that would allow you to run your poller way less frequently than every second. You can find more information here: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html and in the specific SDK documentation. With this you could poll ~every 15-20 seconds or so and wait for messages to arrive.

Thanks,

  • munns
munns
已回答 2 年前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则