Search code examples
node.jsamazon-sqs

sqs does not give me MessageAttributes when receiving message


I have written the following API to recieve message :

app.get('/receive', function(req, res) {
    var params = {
        QueueUrl: queueUrl,
        VisibilityTimeout: 600
    };

    sqs.receiveMessage(params, function(err, data) {
        if (err) {
            res.send(err);
        } else {
            console.log('data is ' + JSON.stringify(data));
            res.send(data);
        }
    });
});

The response that I get is as follows:

{
    "ResponseMetadata": {
        "RequestId": "id"
    },
    "Messages": [
        {
            "MessageId": "id",
            "ReceiptHandle": "handle",
            "MD5OfBody": "body",
            "Body": "body"
        }
    ]
}

.

the send message format for the API is as shown below:

 let params = {
                       MessageBody: object,
                       QueueUrl: process.env.queueUrl,
                        MessageAttributes:{
                           "Title":{
                               DataType:'String',
                               StringValue:'Item Id Array',
                           }
                        },
                       DelaySeconds: 0
                   }
                   sqs.send(params, (err, data) => {
                       console.log('------------the send message data is------------',err,data);
                   });

Solution

  • You must tell SQS which attributes you want to receive in the params passed to receiveMessage function.

    So in your case params definition in /receive route may look like this:

    var params = {
        QueueUrl: queueUrl,
        VisibilityTimeout: 600,
        MessageAttributeNames: ["All"],
    };
    

    Check out the following links for more info:

    1. JavaScript SQS Example
    2. "Calling the receiveMessage operation" example in the AWS JavaScript SDK doc
    3. "MessageAttributeName.N" parameter in the AWS SQS API doc