Amazon Web Services SQS support for Node.js

Support for AWS SQS.


Methods from SDK V2 sqs client

Commands from SDK V3

imported from '@aws-sdk/client-sqs'

Publish
  • sendMessage

  • sendMessageBatch
  • SendMessageCommand
Receive
  • receiveMessage
  • ReceiveMessageCommand

Examples

SDK v2

This code will publish a message into the “SQS_QUEUE_URL” queue:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {
   // Remove DelaySeconds parameter and value for FIFO queues
  DelaySeconds: 10,
  MessageAttributes: {
    "Title": {
      DataType: "String",
      StringValue: "The Whistler"
    },
    "Author": {
      DataType: "String",
      StringValue: "John Grisham"
    },
    "WeeksOn": {
      DataType: "Number",
      StringValue: "6"
    }
  },
  MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.",
  // MessageDeduplicationId: "TheWhistler",  // Required for FIFO queues
  // MessageGroupId: "Group1",  // Required for FIFO queues
  QueueUrl: "SQS_QUEUE_URL"
};

sqs.sendMessage(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.MessageId);
  }
});

This code will receive a message from the queue “SQS_QUEUE_URL”:

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region 
AWS.config.update({region: 'REGION'});

// Create an SQS service object
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});

var params = {
   // Remove DelaySeconds parameter and value for FIFO queues
  DelaySeconds: 10,
  MessageAttributes: {
    "Title": {
      DataType: "String",
      StringValue: "The Whistler"
    },
    "Author": {
      DataType: "String",
      StringValue: "John Grisham"
    },
    "WeeksOn": {
      DataType: "Number",
      StringValue: "6"
    }
  },
  MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016.",
  // MessageDeduplicationId: "TheWhistler",  // Required for FIFO queues
  // MessageGroupId: "Group1",  // Required for FIFO queues
  QueueUrl: "SQS_QUEUE_URL"
};

sqs.receiveMessage(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data.MessageId);
  }
});

The code listed above will produce the following results:

Click to enlarge

When the evaluation of the queue name fails, a Node.js AWS SQS Unknown Publisher (or Receiver) object will be created.

SDK v3

This code will publish a message into the “SQS_QUEUE_URL” queue:

const { SQSClient } = require("@aws-sdk/client-sqs");
const REGION = "us-east-1";
const sqsClient = new SQSClient({ region: REGION });
export  { sqsClient };

const { SendMessageCommand } = require("@aws-sdk/client-sqs");

const params = {
  DelaySeconds: 10,
  MessageAttributes: {
    Title: {
      DataType: "String",
      StringValue: "The Whistler",
    },
    Author: {
      DataType: "String",
      StringValue: "John Grisham",
    },
    WeeksOn: {
      DataType: "Number",
      StringValue: "6",
    },
  },
  MessageBody: "Information about current NY Times fiction bestseller for week of 12/11/2016."
  QueueUrl: "SQS_QUEUE_URL"

};

const run = async () => {
  try {
    const data = await sqsClient.send(new SendMessageCommand(params));
    console.log("Success, message sent. MessageID:", data.MessageId);
    return data; // For unit tests.
  } catch (err) {
    console.log("Error", err);
  }
};
run();

This code will receive a message from the queue “SQS_QUEUE_URL”:

const { SQSClient } = require("@aws-sdk/client-sqs");
const REGION = "us-east-1";
const sqsClient = new SQSClient({ region: REGION });
export  { sqsClient };

const { ReceiveMessageCommand } = require("@aws-sdk/client-sqs");

const queueURL = 'SQS_QUEUE_URL'; 
const params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 10,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  VisibilityTimeout: 20,
  WaitTimeSeconds: 0,
};

const receive = async () => {
  try {
    const data = await sqsClient.send(new ReceiveMessageCommand(params));
    if (data.Messages) {
      console.log("messages obtained");
    } else {
      console.log("No messages");
    }
    return data; // For unit tests.
  } catch (err) {
    console.log("Receive Error", err);
  }
};
receive();

The code listed above will produce the following results:

When the evaluation of the queue name fails, a Node.js AWS SQS Unknown Publisher (or Receiver) object will be created.

Limitations

  • The use of AWS.SQS with promises is not supported. For instance no link would be created between the receiver and the handler function defined in .then() call in the following source code: 
sqs.receiveMessage(params).promise().then( () => {});
  • The use of AWS.SQS with send() is not supported. For instance no link would be created between the receiver and the handler function defined in .send() call in the following source code: 
var request = sqs.receiveMessage(params);
request.send(() => {});