Message Queue Management with AWS SQS (Amazon Simple Queue Service v3) and Node.js

Prashant Suthar
3 min readMay 24, 2024

--

Message Queue Management with AWS SQS (Amazon Simple Queue Service) and Node.js — by PrashantSuthar
Image Source: AWS

Introduction:

Amazon Simple Queue Service (SQS) is a cornerstone of building scalable and resilient distributed systems on the cloud. With its fully managed message queuing capabilities, SQS enables seamless communication between different components of an application, decoupling them and improving overall system reliability. In this article, we’ll delve into harnessing the power of SQS in conjunction with Node.js, utilizing the AWS SDK to facilitate efficient message queue management.

Message Queue Management with AWS SQS (Amazon Simple Queue Service) and Node.js — by PrashantSuthar
Image Source: SYSDIG

Setting up AWS SQS with Node.js:

Before we dive into coding, let’s ensure our development environment is properly configured. Begin by installing the AWS SDK for Node.js using npm, the Node.js package manager:

npm install @aws-sdk/client-sqs

Now, let’s import the necessary modules from the SDK and other dependencies to kickstart our development:

const {
DeleteMessageCommand,
SQSClient,
ReceiveMessageCommand,
ChangeMessageVisibilityCommand,
SendMessageCommand
} = require('@aws-sdk/client-sqs');
var randomstring = require("randomstring");

Configuring AWS Credentials and SQS Endpoint:

To interact with SQS from our Node.js application, we need to provide AWS credentials and specify the SQS endpoint. We define our AWS account ID, the region where our SQS queue resides, and the name of the queue:

const awsCustomerId = '00000000000';
const awsQueue = 'sample-queue.fifo';
const awsRegion = 'eu-west-1';

const queueUrl = `https://sqs.${awsRegion}.amazonaws.com/${awsCustomerId}/${awsQueue}`;
const visibilityTimeout = 60;

const sqsClient = new SQSClient({
credentials: {
accessKeyId:"XXXXXX",
secretAccessKey:"XXXXXX"
},
endpoint: `https://sqs.${awsRegion}.amazonaws.com`,
region: `${awsRegion}`,
apiVersion: '2012-11-05'
});

Sending Messages to SQS:

Now that our environment is set up, let’s start interacting with SQS. We’ll define a function to send a message to our SQS queue. Each message is serialized to JSON format and accompanied by a unique deduplication ID and a message group ID to ensure message integrity and grouping:

async function sendMessage() {
const command = new SendMessageCommand({
QueueUrl: queueUrl,
MessageBody: JSON.stringify({
key1: "value1",
key2: "value2",
}),
MessageDeduplicationId: randomstring.generate(7),
MessageGroupId: randomstring.generate(7),
});

try {
const response = await sqsClient.send(command);
console.log(">>> sendMessage RESPONSE>>>", response)
} catch (error) {
console.log(">>> sendMessage ERROR>>>", error)
throw new Error(error);
}
}

Receiving and Managing Messages:

In a distributed system, it’s crucial to efficiently handle incoming messages. We’ll define functions to retrieve, update visibility, and delete messages from our SQS queue as needed:

async function getMessage() {
// Implementation of getMessage function
}

async function updateMessageVisibility(queueUrl, receiptHandle, visibilityTimeout) {
// Implementation of updateMessageVisibility function
}

async function deleteMessage(receiptHandle) {
// Implementation of deleteMessage function
}

Putting It All Together:

With our message-sending, receiving, and management functions in place, let’s orchestrate their execution within an asynchronous function to demonstrate a typical workflow:

const fn = async()=>{
await sendMessage();
let receiptHandle = await getMessage();
await deleteMessage(receiptHandle);
}

fn();

Working Demo:

For those eager to get hands-on experience, a working demo of this code is available on GitHub. Feel free to pull the repository and explore the code firsthand: GitHub Repository

Conclusion:

In conclusion, integrating Amazon SQS with Node.js empowers developers to build robust and scalable applications that seamlessly handle message-based communication. Leveraging the AWS SDK, developers can harness the full potential of SQS, unlocking the benefits of decoupled architectures and enhancing system reliability. By following the principles outlined in this article, developers can streamline message queue management, laying a solid foundation for building resilient cloud-native applications.

I hope you enjoyed this article! You can follow me Prashant Suthar for more such articles visit prashantsuthar.com. Thanks for reading 🙏 Keep learning 🧠 Keep Sharing 🤝 Stay Awesome 🤘

--

--

Prashant Suthar
Prashant Suthar

Written by Prashant Suthar

I'm Prashant Suthar, a MEAN stack developer specializing in Node.js, AWS Lambda, PHP frameworks, MongoDB, MySQL, Redis, API dev, real-time apps, and DevOps.

No responses yet