Using Queues in Amazon SQS (original) (raw)
This Node.js code example shows:
- How to get a list of all of your message queues
- How to obtain the URL for a particular queue
- How to create and delete queues
About the Example
In this example, a series of Node.js modules are used to work with queues. The Node.js modules use the SDK for JavaScript to enable queues to call the following methods of theAWS.SQS
client class:
For more information about Amazon SQS messages, see How Queues Work in the Amazon Simple Queue Service Developer Guide.
Prerequisite Tasks
To set up and run this example, you must first complete these tasks:
- Install Node.js. For more information about installing Node.js, see the Node.js website.
- Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Loading Credentials in Node.js from the Shared Credentials File.
Listing Your Queues
Create a Node.js module with the file name sqs_listqueues.js
. Be sure to configure the SDK as previously shown. To access Amazon SQS, create anAWS.SQS
service object. Create a JSON object containing the parameters needed to list your queues, which by default is an empty object. Call thelistQueues
method to retrieve the list of queues. The callback returns the URLs of all 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);
}
});
To run the example, type the following at the command line.
node sqs_listqueues.js
This sample code can be found here on GitHub.
Creating a Queue
Create a Node.js module with the file name sqs_createqueue.js
. Be sure to configure the SDK as previously shown. To access Amazon SQS, create anAWS.SQS
service object. Create a JSON object containing the parameters needed to list your queues, which must include the name for the queue created. The parameters can also contain attributes for the queue, such as the number of seconds for which message delivery is delayed or the number of seconds to retain a received message. Call the createQueue
method. The callback returns the URL of the created 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);
}
});
To run the example, type the following at the command line.
node sqs_createqueue.js
This sample code can be found here on GitHub.
Getting the URL for a Queue
Create a Node.js module with the file name sqs_getqueueurl.js
. Be sure to configure the SDK as previously shown. To access Amazon SQS, create anAWS.SQS
service object. Create a JSON object containing the parameters needed to list your queues, which must include the name of the queue whose URL you want. Call the getQueueUrl
method. The callback returns the URL of the specified 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);
}
});
To run the example, type the following at the command line.
node sqs_getqueueurl.js
This sample code can be found here on GitHub.
Deleting a Queue
Create a Node.js module with the file name sqs_deletequeue.js
. Be sure to configure the SDK as previously shown. To access Amazon SQS, create anAWS.SQS
service object. Create a JSON object containing the parameters needed to delete a queue, which consists of the URL of the queue you want to delete. Call the deleteQueue
method.
// 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);
}
});
To run the example, type the following at the command line.
node sqs_deletequeue.js
This sample code can be found here on GitHub.