How To Amazon SQS Part-2 (Consuming Message)

How To Amazon SQS Part-2 (Consuming Message)

Introduction

In the previous part i.e. How To Amazon SQS Part-1 (Sending Message), we had a look at how to send messages in the SQS. You can also call this the producer of the message. In this part, we will see how can we connect to SQS and receive a message for further processing.

Pre-requisites

You should have followed the previous part of the article and can produce messages to an 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 that 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_id 
export AWS_SECRET_ACCESS_KEY=your_secret_access_key 
export 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 a callback. if you wish to achieve the implementation using promise, the following is the code.


/* 
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).promise()
.then(data => {
  console.log(data);
  // do the processing here and delete the message once done
});

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

Conclusion

AWS SQS is a powerful messaging service that 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.

Subscribe to AWSMAG

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe