SQS Send message using javascript


// Load the AWS SDK for Node.js
const AWS = require("aws-sdk");
const sqs = new AWS.SQS({apiVersion: "2012-11-05"});
const params = {
  "MessageBody": JSON.stringify({
    “orderId”: “this-is-an-order-id”,
    “date”: “20200202“,
    “shipBy”: “20200204”,
    "foo": "bar"
  }),
  "QueueUrl": "ADD YOUR QUEUE URL HERE"
};
// By using Callback
sqs.sendMessage(params, (err, data) => {
  if (err) {
    console.log("There was an Error: ", err);
  } else {
    console.log("Successfully added message to queue", data.MessageId);
  }
});

// Promise implementation
sqs.sendMessage(params).promise()
.then(data => console.log("Successfully added message to queue", data.MessageId))
.catch(err => console.log("There was an Error: ", err));

Resources:


I would love to hear what you think about this snippet and if there is anything you would like me to add in future. Drop me a line on @awsmag.