
This assumes you are familiar with the basic configurations needed to connect to Amazon Web Services (AWS) using NodeJS. If not, check out my article Getting Started with NodeJS aws-sdk.
The @aws-sdk/client-sqs package is used to interact with AWS Simple Queue Service (SQS) in NodeJS. The npm list command can be used to determine if you have the @aws-sdk/client-sqs package installed. If not, the npm install command can be used to install the package. Once installed, your package.json should include the @aws-sdk/client-sqs package.
{
"dependencies": {
"@aws-sdk/client-sqs": "^3.693.0"
}
}
This assumes you are already familiar with how to list your AWS Simple Queue Service (SQS) Queues using CommonJS or ES Module. In this example, my-first-queue.fifo will be purged.
import { SQSClient, ListQueuesCommand, PurgeQueueCommand } from "@aws-sdk/client-sqs";
var params
var command
var response
const client = new SQSClient({ region: "us-east-1" })
params = {
QueueNamePrefix: "my-first-queue.fifo"
}
command = new ListQueuesCommand(params)
response = await client.send(command)
params = {QueueUrl: response.QueueUrls.toString()}
command = new PurgeQueueCommand(params)
response = await client.send(command)
console.log(`JSON.stringify(response) = ${JSON.stringify(response)}`)
Here is an example using an async function.
import { SQSClient, ListQueuesCommand, GetQueueAttributesCommand, PurgeQueueCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
const QueueNamePrefix = "my-first-queue.fifo"
async function main() {
const params = {QueueNamePrefix: QueueNamePrefix};
const command = new ListQueuesCommand(params);
const response = await client.send(command);
var queue_url = ""
var message = ""
var messages_count = ""
// --------------------------------------------
// exit if response.QueueUrls.length is not 1
// --------------------------------------------
if (response.QueueUrls.length != 1) {
message = `response.QueueUrls.length is expected to be 1 meaning there is one and only one match found with queue name ${QueueNamePrefix} but response.QueueUrls.length is ${response.QueueUrls.length}`
console.error(`[${strftime('%m/%d/%Y %H:%M:%S', now)} ERROR] ${message}`)
return
} else {
queue_url = response.QueueUrls.toString()
console.log(`[${strftime('%m/%d/%Y %H:%M:%S', now)} INFO] queue URL = ${queue_url}`)
}
// ---------------------------------------
// GET THE COUNT OF MESSAGES IN THE QUEUE
// ---------------------------------------
GetQueueAttributes(queue_url)
.then(
response => {
message = `There were approximately ${response.Attributes.ApproximateNumberOfMessages} messages in ${queue_url}`
console.log(message)
if (response.Attributes.ApproximateNumberOfMessages <= 0) {
return
}
}
)
.catch(
err => {
message = `Got the following error when attempting to Get the Attributes of SQS Queue {queue_url} => ${err}`
console.error(`[${strftime('%m/%d/%Y %H:%M:%S', now)} ERROR] ${message}`)
return
}
)
// ---------------------------------------
// PURGE THE MESSAGES IN THE QUEUE
// ---------------------------------------
PurgeQueue(queue_url)
.then(
response => {
console.log(`purge queue response = ${response}`)
}
)
.catch(
err => {
message = `Got the following error when attempting to Purge the SQS Queue {queue_url} => ${err}`
console.error(`[${strftime('%m/%d/%Y %H:%M:%S', now)} ERROR] ${message}`)
return
}
)
}
async function GetQueueAttributes(queue_url) {
const params = {
QueueUrl: queue_url,
AttributeNames: [ "ApproximateNumberOfMessages" ]
}
const command = new GetQueueAttributesCommand(params);
return await client.send(command);
}
async function PurgeQueue(queue_url) {
const params = {
QueueUrl: queue_url
}
const command = new PurgeQueueCommand(params);
return await client.send(command);
}
main()
Something like this should be returned.
{"$metadata":{"httpStatusCode":200,"requestId":"1b89dc90-dc6d-5fbc-bdb0-835448c24f6a","attempts":1,"totalRetryDelay":0}}
Did you find this article helpful?
If so, consider buying me a coffee over at