There are more AWS SDK examples available in the AWS Doc SDK Examples
Amazon SQS examples using SDK for JavaScript (v2)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for JavaScript (v2) with Amazon SQS.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use ChangeMessageVisibility
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Receive an Amazon SQS message and change its timeout visibility.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region to us-west-2 AWS.config.update({ region: "us-west-2" }); // Create the SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var queueURL = "https://sqs.REGION.amazonaws.com/ACCOUNT-ID/QUEUE-NAME"; var params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 1, MessageAttributeNames: ["All"], QueueUrl: queueURL, }; sqs.receiveMessage(params, function (err, data) { if (err) { console.log("Receive Error", err); } else { // Make sure we have a message if (data.Messages != null) { var visibilityParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, VisibilityTimeout: 20, // 20 second timeout }; sqs.changeMessageVisibility(visibilityParams, function (err, data) { if (err) { console.log("Delete Error", err); } else { console.log("Timeout Changed", data); } }); } else { console.log("No messages to change"); } } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see ChangeMessageVisibility in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use CreateQueue
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Create an Amazon SQS standard queue.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = { QueueName: "SQS_QUEUE_NAME", Attributes: { DelaySeconds: "60", MessageRetentionPeriod: "86400", }, }; sqs.createQueue(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });
Create an Amazon SQS queue that waits for a message to arrive.
// 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); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see CreateQueue in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use DeleteMessage
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Receive and delete Amazon SQS messages.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var queueURL = "SQS_QUEUE_URL"; var params = { AttributeNames: ["SentTimestamp"], MaxNumberOfMessages: 10, MessageAttributeNames: ["All"], QueueUrl: queueURL, VisibilityTimeout: 20, WaitTimeSeconds: 0, }; sqs.receiveMessage(params, function (err, data) { if (err) { console.log("Receive Error", err); } else if (data.Messages) { var deleteParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle, }; sqs.deleteMessage(deleteParams, function (err, data) { if (err) { console.log("Delete Error", err); } else { console.log("Message Deleted", data); } }); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see DeleteMessage in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use DeleteQueue
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Delete an Amazon SQS queue.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = { QueueUrl: "SQS_QUEUE_URL", }; sqs.deleteQueue(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see DeleteQueue in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use GetQueueUrl
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Get the URL for an Amazon SQS queue.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = { QueueName: "SQS_QUEUE_NAME", }; sqs.getQueueUrl(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrl); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see GetQueueUrl in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use ListQueues
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. List your Amazon SQS queues.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = {}; sqs.listQueues(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.QueueUrls); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see ListQueues in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use ReceiveMessage
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Receive a message from an Amazon SQS queue using long-poll support.
// 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); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see ReceiveMessage in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use SendMessage
.
- SDK for JavaScript (v2)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. Send a message to an Amazon SQS queue.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create an SQS service object var sqs = new AWS.SQS({ apiVersion: "2012-11-05" }); var params = { // Remove DelaySeconds parameter and value for FIFO queues DelaySeconds: 10, MessageAttributes: { Title: { DataType: "String", StringValue: "The Whistler", }, Author: { DataType: "String", StringValue: "John Grisham", }, WeeksOn: { DataType: "Number", StringValue: "6", }, }, MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.", // MessageDeduplicationId: "TheWhistler", // Required for FIFO queues // MessageGroupId: "Group1", // Required for FIFO queues QueueUrl: "SQS_QUEUE_URL", }; sqs.sendMessage(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.MessageId); } });
-
For more information, see AWS SDK for JavaScript Developer Guide.
-
For API details, see SendMessage in AWS SDK for JavaScript API Reference.
-