In this article, You will learn how to use LocalStack SQS with Node JS.
We recommend you to read our previous article. If you are not familiar with the LocalStack SQS Testing using AWS CLI.
In this article, We will show you how you can use LocalStack to test AWS SQS (Simple Queue Service) on your local…
We hope you have installed Docker on your system else you can check the following article to install Docker.
Setting Up Docker and Kitematic MacOS/Windows
Installation/Setting Up Docker, Kitematic and VirtualBox MacOS/Windows
Make Sure, You have configured AWS CLI on your system. If not please read the following article.
Install AWS Client
In this article, We will show you how to set up the AWS client
You can create a new Node JS project with the following structure
+ Project Name
Note: Starting with version
0.11.0, all APIs are exposed via a single edge service, which is accessible on http://localhost:4566 by default. If you look into the following file we have used EDGE_PORT=4566.
In the above file, We are creating Single Service as below with default region ap-southeast-1
If you run the following command
You will see the LocalStack container is running successfully as shown below in the screenshot
How To Test Services
To check all the services type the following URL on your browser
So now, the SQS service is running on the local system with the help of LocalStack
Create a New File package.json
Run the following command in the terminal
npm run install
Create an SQS Queue Locally Using Node.JS
Once your LocalStack container is running up. Create the new file util.js
Using Queues in Amazon SQS
In the above file, we have created the SQS configuration instance. So we can reuse the SQS in different files.
Create a new file create.js
In the above file, we have created 2 methods
Which will show a list of queues in the LocalSatck SQS service
This method will create a new queue in the LocalSatck SQS service
Create a New File index.js
Import the previously created file create.js in the index.js file
Now run the following command
It will create and display you the list of SQS Queues as shown below in the screenshot
In the above screenshot, we have an SQS queue URL. Which will be used to create SQS messages
Let’s update the util.js file
In the above file, If you noticed we have added new variables and methods
We will use the above-exported variables and methods to create/read new messages in the LocalStack SQS Queue
Update the create.js file
In the above file, we have created 2 new methods
sendMessage: This method will be used to send the message to the SQS queue.
getMessage: This method will be used to read the message from the SQS queue
Update the index.js file
In the above file, we have commented listSQS and createSqsQueue. Because we have already created the Queue we want to test these methods only
Try to run 1st method sendMessage method by using the following command
This will send the new message to the SQS queue
Then try to run the 2nd method getMessage using the above command. You will see this method will read the message from the SQS queue as shown in the following screenshot.
We are done with LocalStack SQS testing using Node JS
LocalStack SQS Node JS. Contribute to oxlb/localstack-sqs-nodejs development by creating an account on GitHub.
Thank you 🚀🚀🚀🚀🚀🚀