如何让nodejs服务器监听AWS SQS?

发布于 2024-10-15 08:54:56 字数 249 浏览 1 评论 0原文

在详细解释这个问题之前,我先告诉大家我目前的做法。

我有一个运行 setInterval() 的 js 脚本。 在每个时间间隔,我都会调用 SQS 从队列中获取消息。如果有消息,我会处理它。
它无限运行,直到我终止该进程。

我之前也构建过一个节点服务器(使用nodejs.org上的示例)。

所以,我想知道的是,有没有一种方法可以检测 SQS 中是否有新消息,然后触发一个事件并处理该消息,而不是让 setInterval 无限运行?

Before I explain the problem in detail, let me tell you my current approach.

I have a js script that run setInterval().
At each interval, I call SQS to get the message from queue. If there is a message, then I process it.
It runs infinitely until I kill the process.

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

So, what I'm wondering is that instead of having the setInterval to run infinitely, is there a way that to detect if there is a new message in the SQS, which then fires an event and process the message?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-10-22 08:54:56

这个问题已经有两年多了..但是有一个比改变轮询间隔更好的方法。而是将队列的接收消息等待时间设置为最大值 20 秒。然后你可以进行连续轮询,但在队列为空时每分钟只会发出 3 个请求。当队列中有数据时,将立即响应。

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.

吃不饱 2024-10-22 08:54:56

不可以。您必须向 SQS 请求消息。

如果您确实需要推送通知,请查看 SNS。如果您希望在将消息添加到队列后向服务器提示轮询 SQS,则 SNS 效果很好。

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.

櫻之舞 2024-10-22 08:54:56

您可以使用 SQS 长轮询 来实现此目的。长轮询允许 Amazon SQS 等到队列中有可用消息后再发送响应,从而减少空响应的数量。这也将显着降低 SQS 的成本。

为了让这一切变得简单,有一个名为 sqs-consumer 的优秀库。它允许您定义一个接收 SQS 消息的函数,并在处理消息时调用回调:

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();

在幕后,它已经利用了上述的长轮询技术。

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.

爱*していゐ 2024-10-22 08:54:56

SQS 不提供通知,但如果 SQS 中有新消息,您是否可以让创建该消息的任何内容也访问 node.js 并打开轮询几分钟。您可能无法控制队列中的内容,但如果您可以控制,我也会触发您的 node.js 开始轮询队列。

如果您因成本而担心轮询,您可以按照我的做法 - 动态更改您的轮询时间。我的 SQS 队列每 5 秒轮询一次。如果节点检测到消息,它会立即将轮询时间降低到 200 毫秒,持续几秒钟。如果它没有检测到队列中的消息,则每个空请求都会减慢 50 毫秒,直到再次命中 5 秒轮询。

第一个请求会很慢,您可能无法处理。为了解决这个问题,我每隔几分钟随机加快一次轮询时间。通过几个节点轮询,响应时间通常非常快。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文