how to receive message in aws sqs node js
Asked Answered
A

3

9

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

Amoebocyte answered 31/1, 2020 at 15:11 Comment(4)
Can you show the response from receiveMessage?Detumescence
"ResponseMetadata": { "RequestId": "8659872b-10f0-57b6-9d57-d1852aba1a64" } this is the responseAmoebocyte
@UsmanSaleem did my answer fix your problem?Newsman
Hi @UsmanSaleem, I understand this is an old question, I ran into the same issue today, did you ever find a resolution to your issue?Disbelief
S
3

You need to set WaitTimeSeconds: 20 to make a long polling

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: 20
        };

        sqs.receiveMessage(params, function (err, data) {
            if (err) {
                res.send(err);
            } else {
                res.send(data);
            }
        });
    }
});
Serrano answered 22/9, 2020 at 1:16 Comment(0)
N
2

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
Newsman answered 31/1, 2020 at 16:10 Comment(2)
Strange that the getQueueUrl() call does not yield a MultipleValidationErrors error with InvalidParameterType and MissingRequiredParameter among other things. That's what I see.Rodgers
I still only get the ResponseMetadata in my request. I have tried both sqs.getQueueUrl() and using the URL from a string. Any more ideas? This was working a week ago for me with no code changes.Hanseatic
I
0

If you have more than 1000 messages in your queue, you need to set WaitTimeSeconds larger than 0. Default is 0.

See the difference between short polling and long polling here:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-short-and-long-polling.html#sqs-long-polling

Short polling may return empty even you have messages in the queue.

Infernal answered 14/7, 2020 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.