Managing visibility timeout in Amazon SQS - AWS SDK for JavaScript

Help us improve the AWS SDK for JavaScript version 3 (V3) documentation by providing feedback using the Feedback link, or create an issue or pull request on GitHub.

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (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 AWS Region.

import { SQSClient } from "@aws-sdk/client-sqs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create SNS 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 { var visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20, // 20 second timeout }; const results = await sqsClient.send( new ChangeMessageVisibilityCommand(params) ); 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.