Menu
AWS SDK for JavaScript
Developer Guide for SDK v2.152.0

Enabling Long Polling in Amazon SQS


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to enable long polling in Amazon SQS in one of these ways:

  • For a newly created queue

  • For an existing queue

  • Upon receipt of a message

The Scenario

Long polling reduces the number of empty responses by allowing Amazon SQS to wait a specified time for a message to become available in the queue before sending a response. Also, long polling eliminates false empty responses by querying all of the servers instead of a sampling of servers. To enable long polling, you must specify a non-zero wait time for received messages. You can do this by setting the ReceiveMessageWaitTimeSeconds parameter of a queue or by setting the WaitTimeSeconds parameter on a message when it is received.

In this example, a series of Node.js modules are used to enable long polling. The Node.js modules use the SDK for JavaScript to enable long polling using these methods of the AWS.SQS client class:

For more information about Amazon SQS long polling, see Long Polling in the Amazon Simple Queue Service Developer Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object then setting the region for your code. In this example, the region is set to us-west-2.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'us-west-2'});

Enabling Long Polling When Creating a Queue

Create a Node.js module with the file name sqs_longpolling_createqueue.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to create a queue, including a non-zero value for the ReceiveMessageWaitTimeSeconds parameter. Call the createQueue method. Long polling is then enabled for the queue.

Copy
// 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); } });

To run the example, type the following at the command line.

Copy
node sqs_longpolling_createqueue.js

This sample code can be found here on GitHub.

Enabling Long Polling on an Existing Queue

Create a Node.js module with the file name sqs_longpolling_existingqueue.js. Be sure to configure the SDK as previously shown. To access Amazon Simple Queue Service, create an AWS.SQS service object. Create a JSON object containing the parameters needed to set the attributes of queue, including a non-zero value for the ReceiveMessageWaitTimeSeconds parameter and the URL of the queue. Call the setQueueAttributes method. Long polling is then enabled for the queue.

Copy
// 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); } });

To run the example, type the following at the command line.

Copy
node sqs_longpolling_existingqueue.js

This sample code can be found here on GitHub.

Enabling Long Polling on Message Receipt

Create a Node.js module with the file name sqs_longpolling_receivemessage.js. Be sure to configure the SDK as previously shown. To access Amazon Simple Queue Service, create an AWS.SQS service object. Create a JSON object containing the parameters needed to receive messages, including a non-zero value for the WaitTimeSeconds parameter and the URL of the queue. Call the receiveMessage method.

Copy
// 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); } });

To run the example, type the following at the command line.

Copy
node sqs_longpolling_receivemessage.js

This sample code can be found here on GitHub.