LocalStack SQS Node JS

In this article, You will learn how to use LocalStack SQS with Node JS.

LocalStack SQS Node.JS

We recommend you to read our previous article. If you are not familiar with the LocalStack SQS Testing using AWS CLI.

Video

Let’s Start

We hope you have installed Docker on your system else you can check the following article to install Docker.

Make Sure, You have configured AWS CLI on your system. If not please read the following article.

Project Structure

You can create a new Node JS project with the following structure

+ Project Name
- create.js
- docker-compose.yml
- index.js
- package.json
- util.js

Docker-Compose.YML

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

  1. SQS

If you run the following command

docker-compose up

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

http://localhost:4566/health
Loclstack SQS Docker Compose

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

Reference

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

1. listSQS:

Which will show a list of queues in the LocalSatck SQS service

2. createSqsQueue:

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

node index.js

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

1. QUEUE_URL

2. msgParams

3. receiveParam

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

  1. sendMessage
  2. getMessage

Try to run 1st method sendMessage method by using the following command

node index.js

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

Github

Thank you 🚀🚀🚀🚀🚀🚀

Teaching & Developing For Startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store