Menu
AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Sending and Receiving Messages in Amazon SQS


                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to send, receive, and delete messages in a queue.

The Scenario

In this example, a series of Node.js modules are used to send and receive messages. The Node.js modules use the SDK for JavaScript to send and receive messages by using these methods of the AWS.SQS client class:

For more information about Amazon SQS messages, see Sending a Message to an Amazon SQS Queue and Receiving and Deleting a Message from an Amazon SQS Queue in the Amazon Simple Queue Service Developer Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object, setting the region, and providing credentials for your code. In this example, the credentials are provided by using the JSON file you created.

Copy
// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set the region from the JSON file AWS.config.loadFromPath('./config.json');

Sending a Message to a Queue

Create a Node.js module with the file name sqs_sendmessage.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed for your message, including the URL of the queue to which you want to send this message. In this example, the message provides details about a book on a list of fiction best sellers including the title, author, and number of weeks on the list.

Call the sendMessage method. The callback returns the unique ID of the message.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set the region from the JSON file AWS.config.loadFromPath('./config.json'); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var 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" }; sqs.sendMessage(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.MessageId); } });

To run the example, type the following at the command line.

Copy
node sqs_sendmessage.js

This sample code can be found here on GitHub.

Receiving and Deleting Messages from a Queue

Create a Node.js module with the file name sqs_receivemessage.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed for your message, including the URL of the queue from which you want to receive messages. In this example, the parameters specify receipt of all message attributes, as well as receipt of no more than 10 messages.

Call the receiveMessage method. The callback returns an array of Message objects from which you can retrieve ReceiptHandle for each message that you use to later delete that message. Create another JSON object containing the parameters needed to delete the message, which are the URL of the queue and the ReceiptHandle value. Call the deleteMessage method to delete the message you received.

Copy
// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set the region from the JSON file AWS.config.loadFromPath('./config.json'); // Create an SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var queueURL = "SQS_QUEUE_URL"; var params = { AttributeNames: [ "SentTimestamp" ], MaxNumberOfMessages: 1, MessageAttributeNames: [ "All" ], QueueUrl: queueURL, VisibilityTimeout: 0, WaitTimeSeconds: 0 }; sqs.receiveMessage(params, function(err, data) { if (err) { console.log("Receive Error", err); } else { var deleteParams = { QueueUrl: queueURL, ReceiptHandle: data.Messages[0].ReceiptHandle }; sqs.deleteMessage(deleteParams, function(err, data) { if (err) { console.log("Delete Error", err); } else { console.log("Message Deleted", data); } }); } });

To run the example, type the following at the command line.

Copy
node sqs_receivemessage.js

This sample code can be found here on GitHub.