Getting Started with AWS SQS using Node.js - Part 1
Tags: aws, node, javascript
Published: 2020-03-09 07:03:39

## Introduction
Amazon SQS (Simple Queue Service) is a fully managed message queue service which enables you to write distributed applications. Using SQS, you can send and receive message between many components in heavy volume. SQS allows you to decouple and scale microservices, write event driven application and also enables the server less application. It removes the complexity of managing the message driven architectures.
In this part we will see how can we connect to SQS and send message to it.

## Pre-requisites
You will need a valid AWS account and credentials to access the SQS. You will also need to have access to the AWS console to create a SQS.

## Setting up a queue
To setup a queue you first login to AWS and navigate to SQS. Follow the instruction to create a queue. Once created, you will need the URL property of the SQS to use in the code. Make sure, the credentials you are using have access to send and receive message from the SQS.

## The Application Flow
Let’s assume we are building an application to process the e-commerce orders. Once the order is successful, the order service will send a message to fulfilment service and shipping service to make sure the right products are gathered and packed for shipment.

## Sending a message
Let’s assume these are the details for the fulfilment service.

```
{
  “orderId”: “this-is-an-order-id”,
  “date”: “2020–02–02“,
  “shipBy”: “2020–02–04”,
  "foo": "bar"
}
```
You can include other relevant data as well. The idea is to send maximum allowed information as per your design so that fulfilment service can work on its own to figure out the next steps and perform it efficiently.
Now that we have a message structure, we need to send it out to the desired SQS. Following is the code for sending it out in Node.js. We have to import the AWS SDK for node.js and use it to send a message . The SDK is capable of using the credentials stored in your env. It looks for the following environment variable:-

```
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
export AWS_REGION=the_region_you_are_using
```

Following is the code to send the message:-

```
/* Getting Started with AWS SQS using node js. This part shows how to send message to the SQS */


// Load the AWS SDK for Node.js
const AWS = require("aws-sdk");

const sqs = new AWS.SQS({apiVersion: "2012-11-05"});
const params = {
  "MessageBody": JSON.stringify({
    “orderId”: “this-is-an-order-id”,
    “date”: “2020–02–02“,
    “shipBy”: “2020–02–04”,
    "foo": "bar"
  }),
  "QueueUrl": "ADD YOUR QUEUE URL HERE"
};

// By using Callback
sqs.sendMessage(params, (err, data) => {
  if (err) {
    console.log("There was an Error: ", err);
  } else {
    console.log("Successfully added message to queue", data.MessageId);
  }
});
```

The above is implemented using the callback. if you wish to achieve the implementation using promise, following is the implementation.

```
// Promise implementation
sqs.sendMessage(params).promise()
.then(data => console.log("Successfully added message to queue", data.MessageId))
.catch(err => console.log("There was an Error: ", err));
```

You can also find the code sample in my github repo at the following link
[Code Sample Here](https://github.com/singhs020/examples/blob/master/src/SQS/sendingMessage.js)

## Conclusion
AWS SQS is a powerful messaging service which allows you to use your own creativity to find the right fit for it in your application. In the next part, we will extend it and write a small consumer for the message in our fulfilment service.