Core messaging functionality for sending, receiving, and deleting messages with full support for message attributes, FIFO ordering, and visibility control.
Sends a single message to an SQS queue with optional attributes and delivery delay.
class SendMessageCommand {
constructor(input: SendMessageCommandInput);
}
interface SendMessageCommandInput {
/** URL of the target queue */
QueueUrl: string;
/** Message payload (up to 1 MB) */
MessageBody: string;
/** Delay before message becomes available (0-900 seconds) */
DelaySeconds?: number;
/** Custom message attributes */
MessageAttributes?: Record<string, MessageAttributeValue>;
/** Deduplication ID for FIFO queues */
MessageDeduplicationId?: string;
/** Group ID for FIFO queues */
MessageGroupId?: string;
/** System-defined message attributes */
MessageSystemAttributes?: Record<MessageSystemAttributeNameForSends, MessageSystemAttributeValue>;
}
interface SendMessageCommandOutput {
/** MD5 hash of the message body */
MD5OfBody?: string;
/** MD5 hash of the message attributes */
MD5OfMessageAttributes?: string;
/** MD5 hash of the system attributes */
MD5OfMessageSystemAttributes?: string;
/** Unique message identifier */
MessageId?: string;
/** FIFO sequence number */
SequenceNumber?: string;
}Usage Examples:
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient({ region: "us-east-1" });
// Send basic message
const basicMessage = await client.send(new SendMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MessageBody: "Hello, SQS!"
}));
// Send message with attributes
const messageWithAttributes = await client.send(new SendMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MessageBody: JSON.stringify({
orderId: "12345",
customerId: "customer-abc"
}),
MessageAttributes: {
Author: {
StringValue: "order-service",
DataType: "String"
},
Priority: {
StringValue: "high",
DataType: "String"
},
Timestamp: {
StringValue: new Date().toISOString(),
DataType: "String"
},
Version: {
StringValue: "1.0",
DataType: "Number"
}
}
}));
// Send delayed message
const delayedMessage = await client.send(new SendMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MessageBody: "This message will be delayed",
DelaySeconds: 300 // 5 minutes
}));
// Send FIFO message
const fifoMessage = await client.send(new SendMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyFifoQueue.fifo",
MessageBody: "Order processing",
MessageGroupId: "order-group-1",
MessageDeduplicationId: "order-12345-dedup"
}));
console.log("Message sent with ID:", basicMessage.MessageId);Retrieves one or more messages from a queue with support for long polling and attribute filtering.
class ReceiveMessageCommand {
constructor(input?: ReceiveMessageCommandInput);
}
interface ReceiveMessageCommandInput {
/** URL of the source queue */
QueueUrl: string;
/** Queue attributes to include in response */
AttributeNames?: QueueAttributeName[];
/** Message attribute names to include in response */
MessageAttributeNames?: string[];
/** Maximum number of messages to retrieve (1-10) */
MaxNumberOfMessages?: number;
/** Message visibility timeout in seconds (0-43200) */
VisibilityTimeoutSeconds?: number;
/** Long polling wait time in seconds (0-20) */
WaitTimeSeconds?: number;
/** Token for pagination */
ReceiveRequestAttemptId?: string;
}
interface ReceiveMessageCommandOutput {
/** Array of received messages */
Messages?: Message[];
}
interface Message {
/** Unique message identifier */
MessageId?: string;
/** Receipt handle for message operations */
ReceiptHandle?: string;
/** MD5 hash of message body */
MD5OfBody?: string;
/** Message payload */
Body?: string;
/** System-defined message attributes */
Attributes?: Record<MessageSystemAttributeName, string>;
/** MD5 hash of message attributes */
MD5OfMessageAttributes?: string;
/** Custom message attributes */
MessageAttributes?: Record<string, MessageAttributeValue>;
}Usage Examples:
// Basic message receive
const messages = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue"
}));
// Receive with long polling
const longPollMessages = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MaxNumberOfMessages: 10,
WaitTimeSeconds: 20, // Long poll for up to 20 seconds
MessageAttributeNames: ["All"],
AttributeNames: ["ApproximateReceiveCount", "SentTimestamp"]
}));
// Process received messages
for (const message of longPollMessages.Messages || []) {
console.log("Message ID:", message.MessageId);
console.log("Body:", message.Body);
console.log("Receipt Handle:", message.ReceiptHandle);
// Access message attributes
if (message.MessageAttributes) {
for (const [name, value] of Object.entries(message.MessageAttributes)) {
console.log(`Attribute ${name}:`, value.StringValue);
}
}
// Access system attributes
if (message.Attributes) {
console.log("Sent Timestamp:", message.Attributes.SentTimestamp);
console.log("Receive Count:", message.Attributes.ApproximateReceiveCount);
}
}
// Receive with custom visibility timeout
const customVisibility = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
VisibilityTimeoutSeconds: 120, // 2 minutes
MaxNumberOfMessages: 5
}));Removes a message from the queue using its receipt handle.
class DeleteMessageCommand {
constructor(input: DeleteMessageCommandInput);
}
interface DeleteMessageCommandInput {
/** URL of the queue containing the message */
QueueUrl: string;
/** Receipt handle of the message to delete */
ReceiptHandle: string;
}Usage Examples:
// Receive and delete messages
const received = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MaxNumberOfMessages: 10
}));
for (const message of received.Messages || []) {
try {
// Process message
await processMessage(message.Body);
// Delete after successful processing
await client.send(new DeleteMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
ReceiptHandle: message.ReceiptHandle!
}));
console.log("Message processed and deleted:", message.MessageId);
} catch (error) {
console.error("Failed to process message:", message.MessageId, error);
// Message will remain in queue and become visible again
}
}
async function processMessage(body: string | undefined) {
if (!body) return;
const data = JSON.parse(body);
// Process the message data
console.log("Processing:", data);
}Modifies the visibility timeout of a message, affecting when it becomes available for other consumers.
class ChangeMessageVisibilityCommand {
constructor(input: ChangeMessageVisibilityCommandInput);
}
interface ChangeMessageVisibilityCommandInput {
/** URL of the queue containing the message */
QueueUrl: string;
/** Receipt handle of the message */
ReceiptHandle: string;
/** New visibility timeout in seconds (0-43200) */
VisibilityTimeoutSeconds: number;
}Usage Examples:
// Extend processing time for a message
const received = await client.send(new ReceiveMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
MaxNumberOfMessages: 1
}));
const message = received.Messages?.[0];
if (message) {
try {
// Start processing
await processLongRunningTask(message.Body);
// Extend visibility if processing takes longer than expected
await client.send(new ChangeMessageVisibilityCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
ReceiptHandle: message.ReceiptHandle!,
VisibilityTimeoutSeconds: 600 // Extend to 10 minutes
}));
// Continue processing
await continueProcessing(message.Body);
// Delete when done
await client.send(new DeleteMessageCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
ReceiptHandle: message.ReceiptHandle!
}));
} catch (error) {
// Make message immediately visible for retry
await client.send(new ChangeMessageVisibilityCommand({
QueueUrl: "https://sqs.us-east-1.amazonaws.com/123456789012/MyQueue",
ReceiptHandle: message.ReceiptHandle!,
VisibilityTimeoutSeconds: 0
}));
}
}
async function processLongRunningTask(body: string | undefined) {
// Simulate long processing
await new Promise(resolve => setTimeout(resolve, 5000));
}
async function continueProcessing(body: string | undefined) {
// Continue processing
console.log("Continuing to process:", body);
}interface MessageAttributeValue {
/** String value for the attribute */
StringValue?: string;
/** Binary value for the attribute */
BinaryValue?: Uint8Array;
/** Array of string values */
StringListValues?: string[];
/** Array of binary values */
BinaryListValues?: Uint8Array[];
/** Data type of the attribute value */
DataType: string;
}
interface MessageSystemAttributeValue {
/** String value for system attribute */
StringValue?: string;
/** Binary value for system attribute */
BinaryValue?: Uint8Array;
/** Data type of the system attribute */
DataType: string;
}
enum MessageSystemAttributeName {
SenderId = "SenderId",
SentTimestamp = "SentTimestamp",
ApproximateReceiveCount = "ApproximateReceiveCount",
ApproximateFirstReceiveTimestamp = "ApproximateFirstReceiveTimestamp",
SequenceNumber = "SequenceNumber",
MessageDeduplicationId = "MessageDeduplicationId",
MessageGroupId = "MessageGroupId",
AWSTraceHeader = "AWSTraceHeader"
}
enum MessageSystemAttributeNameForSends {
AWSTraceHeader = "AWSTraceHeader"
}Common exceptions for message operations:
try {
await client.send(new DeleteMessageCommand({
QueueUrl: queueUrl,
ReceiptHandle: expiredHandle
}));
} catch (error) {
if (error.name === 'ReceiptHandleIsInvalid') {
console.log("Receipt handle expired, message already processed by another consumer");
} else if (error.name === 'MessageNotInflight') {
console.log("Message not currently visible for processing");
}
}