Managing visibility timeout in Amazon SQS - Amazon SDK for JavaScript
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

The Amazon SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the Amazon SDK for JavaScript version 3 (V3).

Starting October 1, 2022, Amazon SDK for JavaScript (v3) will end support for Internet Explorer 11 (IE 11). For details, see Announcing the end of support for Internet Explorer 11 in the Amazon SDK for JavaScript (v3).

Managing visibility timeout in Amazon SQS

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to specify the time interval during which messages received by a queue are not visible.

The scenario

In this example, a Node.js module is used to manage visibility timeout. The Node.js module uses the SDK for JavaScript to manage visibility timeout by using this method of the SQS client class:

For more information about Amazon SQS visibility timeout, see Visibility timeout in the Amazon Simple Queue Service Developer Guide.

Prerequisite tasks

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


These examples demonstrate how to import/export client service objects and command using ECMAScript6 (ES6).

Changing the visibility timeout

Create a libs directory, and create a Node.js module with the file name sqsClient.js. Copy and paste the code below into it, which creates the Amazon SQS client object. Replace REGION with your Amazon Region.

import { SQSClient } from "@aws-sdk/client-sqs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SQS service object. const sqsClient = new SQSClient({ region: REGION }); export { sqsClient };

This example code can be found here on GitHub.

Create a Node.js module with the file name sqs_changingvisibility.js. Be sure to configure the SDK as previously shown. Receive the message from the queue.

Upon receipt of the message from the queue, create a JSON object containing the parameters needed for setting the timeout, including the URL of the queue containing the message, the ReceiptHandle returned when the message was received, and the new timeout in seconds. Call the ChangeMessageVisibilityCommand method.


Replace and ACCOUNT_ID with the ID of the account, and QUEUE_NAME with the name of the queue.

// Import required AWS SDK clients and commands for Node.js import { ReceiveMessageCommand, ChangeMessageVisibilityCommand, } from "@aws-sdk/client-sqs"; import { sqsClient } from "./libs/sqsClient.js"; // Set the parameters const queueURL = ""; // REGION, ACCOUNT_ID, QUEUE_NAME const params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueURL, }; const run = async () => { try { const data = await sqsClient.send(new ReceiveMessageCommand(params)); if (data.Messages != null) { try { const visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20, // 20 second timeout }; const results = await sqsClient.send( new ChangeMessageVisibilityCommand(visibilityParams) ); console.log("Timeout Changed", results); } catch (err) { console.log("Delete Error", err); } } else { console.log("No messages to change"); } return data; // For unit tests. } catch (err) { console.log("Receive Error", err); } }; run();

To run the example, enter the following at the command prompt.

node sqs_changingvisibility.js

This example code can be found here on GitHub.