

 適用於 JavaScript 的 AWS SDK v2 已end-of-support。我們建議您遷移至 [適用於 JavaScript 的 AWS SDK v3](https://docs.aws.amazon.com//sdk-for-javascript/v3/developer-guide/)。如需如何遷移的其他詳細資訊和資訊，請參閱此[公告](https://aws.amazon.com/blogs//developer/announcing-end-of-support-for-aws-sdk-for-javascript-v2/)。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 Amazon SQS 中啟用長輪詢
<a name="sqs-examples-enable-long-polling"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v2/developer-guide/images/nodeicon.png)

**這個 Node.js 程式碼範例會說明：**
+ 如何為新建立的佇列啟用長輪詢
+ 如何為現有佇列啟用長輪詢
+ 如何在收到訊息時啟用長輪詢

## 使用案例
<a name="sqs-examples-enable-long-polling-scenario"></a>

長輪詢允許 Amazon SQS 在傳送回應之前等待指定的時間，讓訊息在佇列中變成可用，以減少空回應的數量。此外，長輪詢可查詢所有伺服器而非指查詢取樣的伺服器，來減少假的空白回應。若要啟用長輪詢，您必須指定針對接收的訊息指定非零的等待時間。您可以設定佇列的 `ReceiveMessageWaitTimeSeconds` 參數，或是在收到訊息時設定 `WaitTimeSeconds` 參數，藉此指定等待時間。

在此範例中，您可以使用一系列的 Node.js 模組來啟用長輪詢。Node.js 模組使用適用於 JavaScript 的 SDK，以下列`AWS.SQS`用戶端類別的方法啟用長輪詢：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#setQueueAttributes-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#setQueueAttributes-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#receiveMessage-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#receiveMessage-property)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#createQueue-property](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html#createQueue-property)

如需 Amazon SQS 長輪詢的詳細資訊，請參閱《*Amazon Simple Queue Service 開發人員指南*》中的[長輪詢](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html)。

## 先決條件任務
<a name="sqs-examples-enable-long-polling-prerequisites"></a>

若要設定和執行此範例，您必須先完成這些任務：
+ 安裝 Node.js。如需安裝 Node.js 的詳細資訊，請參閱 [Node.js 網站](https://nodejs.org)。
+ 透過使用者登入資料建立共用組態檔。如需提供共用登入資料檔案的詳細資訊，請參閱 [從共用登入資料檔案中在 Node.js 中載入登入資料](loading-node-credentials-shared.md)。

## 在建立佇列時啟用長輪詢
<a name="sqs-examples-enable-long-polling-on-queue-creation"></a>

以檔名 `sqs_longpolling_createqueue.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。若要存取 Amazon SQS，請建立 `AWS.SQS`服務物件。建立包含所需參數的 JSON 物件以建立佇列，其中應包括 `ReceiveMessageWaitTimeSeconds` 參數的非零值。呼叫 `createQueue` 方法。然後為佇列啟用長輪詢。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var params = {
  QueueName: "SQS_QUEUE_NAME",
  Attributes: {
    ReceiveMessageWaitTimeSeconds: "20",
  },
};

sqs.createQueue(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.QueueUrl);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node sqs_longpolling_createqueue.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_createqueue.js)找到這個範本程式碼。

## 在現有佇列上啟用長輪詢
<a name="sqs-examples-enable-long-polling-existing-queue"></a>

以檔名 `sqs_longpolling_existingqueue.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。若要存取 Amazon Simple Queue Service，請建立 `AWS.SQS`服務物件。建立包含設定佇列屬性所需參數的 JSON 物件，其中應包括 `ReceiveMessageWaitTimeSeconds` 參數的非零值和佇列 URL。呼叫 `setQueueAttributes` 方法。然後為佇列啟用長輪詢。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var params = {
  Attributes: {
    ReceiveMessageWaitTimeSeconds: "20",
  },
  QueueUrl: "SQS_QUEUE_URL",
};

sqs.setQueueAttributes(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node sqs_longpolling_existingqueue.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_existingqueue.js)找到這個範本程式碼。

## 在收到訊息時啟用長輪詢
<a name="sqs-examples-enable-long-polling-on-receive-message"></a>

以檔名 `sqs_longpolling_receivemessage.js` 建立一個 Node.js 模組。請務必依前述的內容來設定軟體開發套件。若要存取 Amazon Simple Queue Service，請建立 `AWS.SQS`服務物件。建立包含接收訊息所需參數的 JSON 物件，其中應包括 `WaitTimeSeconds` 參數的非零值和佇列 URL。呼叫 `receiveMessage` 方法。

```
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });

// Create the SQS service object
var sqs = new AWS.SQS({ apiVersion: "2012-11-05" });

var queueURL = "SQS_QUEUE_URL";

var params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 1,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  WaitTimeSeconds: 20,
};

sqs.receiveMessage(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});
```

若要執行範例，請在命令列中輸入以下內容。

```
node sqs_longpolling_receivemessage.js
```

您可以在 [GitHub 上](https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javascript/example_code/sqs/sqs_longpolling_receivemessage.js)找到這個範本程式碼。