Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to receive message in aws sqs node js

i am implementing sqs in my Node js project. what i am doing is sending msg in SQS and receiving it. but when i receive it is just ResponseMetadata object

   "ResponseMetadata": {
    "RequestId": "8659872b-10f0-57b6-9d57-d1852aba1a64"
    }

there's no Message object in response. what should i do? i have many possibilities like changing param values etc but nothing works.

my code

   const AWS = require('aws-sdk');
   AWS.config.update({ accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY, region: 'eu-west-1' })
   const sqs = new AWS.SQS({apiVersion: '2012-11-05'});

   onst queueUrl = "https://eu-west-1.queue.amazonaws.com/******/test-queue";

send msg

     let params = {
            MessageBody: 'Hello world!',
            MessageAttributes: {
                "Title": {
                    DataType: "String",
                    StringValue: "The Whistler"
                },
                "Author": {
                    DataType: "String",
                    StringValue: "John Grisham"
                },
                "WeeksOn": {
                    DataType: "Number",
                    StringValue: "6"
                }
            },
            QueueUrl: queueUrl,
            DelaySeconds: 0
        };

        sqs.sendMessage(params, function (err, data) {
            if (err) {
                res.send(err);
            } else {
                res.send(data);
            }
        });

response is

     "ResponseMetadata": {
    "RequestId": "da3af650-2642-5460-86b7-a0fe1f9ced6f"
},
"MD5OfMessageBody": "86fb269d190d2c85f6e0468ceca42a20",
"MD5OfMessageAttributes": "1864106991a54cca8b8c732a1841833a",
"MessageId": "13f228b0-7df1-4a9e-bc2b-48535725955e"

receive msg

       sqs.getQueueUrl('queue-name', function(err, data) {
            if (err) {
                console.log("Error", err);
            } else {
                let params = {
                    AttributeNames: [
                        "SentTimestamp"
                    ],
                    MaxNumberOfMessages: 10,
                    VisibilityTimeout: 20,
                    MessageAttributeNames: ["All"],
                    QueueUrl: data.QueueUrl,
                    WaitTimeSeconds: 0
                };

                sqs.receiveMessage(params, function (err, data) {
                    if (err) {
                        res.send(err);
                    } else {
                        res.send(data);
                    }
                });
            }
        });

can someone help? is there anything i am missing? thanks is advance

like image 818
Usman Saleem Avatar asked Jan 31 '20 15:01

Usman Saleem


1 Answers

The getQueueUrl call is not correct. You need to wrap the queue name in an object like this

sqs.getQueueUrl({"QueueName": "queue name"}, function (err, data) {

also, it is worth using promise version instead of callback.

const data = sqs.getQueueUrl({"QueueName": "queue name"}).promise();

// Similary receiveMessage
like image 84
Ashish Modi Avatar answered Nov 15 '22 06:11

Ashish Modi