DEV Community

Simranjeet Singh
Simranjeet Singh

Posted on

Getting Started with AWS SQS using Node.js - Part 2

Introduction

In the previous part i.e. Getting Started with AWS SQS using Node.js - Part 1, we had a look on how to send messages in the SQS. You can also call this as the producer of the message.
In this part we will see how can we connect to SQS and receive message for further processing.

Pre-requisites

You should have followed the previous part of the article and can produce messages to a SQS.

The Application Flow

In the previous part we were building an e-commerce app where an order service is producing messages to the SQS for further processing. In this part we will be looking at a fulfilment service which will receive the message and process it further.

Receiving a message

This was the message which was produced in the last part for fulfilment service

{
  "orderId": "this-is-an-order-id",
  "date": "2020–02–02",
  "shipBy": "2020–02–04",
  "foo": "bar"
}

Like we did last time, 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_idexport
AWS_SECRET_ACCESS_KEY=your_secret_access_keyexport
AWS_REGION=the_region_you_are_using

Following is the code to receive the message:-

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


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

const sqs = new AWS.SQS({apiVersion: "2012-11-05"});

const qurl = "ADD YOUR SQS URL HERE";

const params = {
  "QueueUrl": qurl,
  "MaxNumberOfMessages": 1
};

sqs.receiveMessage(params, (err, data) => {
  if (err) {
    console.log(err, err.stack);
  } else {
    if (!Array.isArray(data.Messages) || data.Messages.length === 0) { 
      console.log("There are no messages available for processing."); 
      return;
    }    

    const body = JSON.parse(data.Messages[0].Body);
    console.log(body);

    // process the body however you see fit.
    // once the processing of the body is complete, delete the message from the SQS to avoid reprocessing it.

    const delParams = {
      "QueueUrl": qurl,
      "ReceiptHandle": data.Messages[0].ReceiptHandle
    };

    sqs.deleteMessage(delParams, (err, data) => {
      if (err) {
        console.log("There was an error", err);
      } else {
        console.log("Message processed Successfully");
      }
    });
  }
});

Do, not forget to delete the message after you are done with your task. This is important to avoid any re-processing of the message. The above is implemented using callback. if you wish to achieve the implementation using promise, following is the code.

// the above message can be implemented using promise as well.
sqs.receiveMessage(params).promise()
.then(data => {
  console.log(data);
  // do the processing here
});

You can also find the code sample in my github repo.

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. The most common way to consume messages is using a polling mechanism which can poll the SQS and process all the message. This is a very basic integration of SQS in an application, there are other advanced use cases too like dead-letter queues, FIFO queues and Lambda integration with SQS to process streams.

Top comments (0)