sendMessage method

Future<SendMessageResult> sendMessage({
  1. required String messageBody,
  2. required String queueUrl,
  3. int? delaySeconds,
  4. Map<String, MessageAttributeValue>? messageAttributes,
  5. String? messageDeduplicationId,
  6. String? messageGroupId,
  7. Map<MessageSystemAttributeNameForSends, MessageSystemAttributeValue>? messageSystemAttributes,
})

Delivers a message to the specified queue.

#x9 | #xA | #xD | #x20 to #xD7FF | #xE000 to #xFFFD | #x10000 to #x10FFFF

If a message contains characters outside the allowed set, Amazon SQS rejects the message and returns an InvalidMessageContents error. Ensure that your message body includes only valid characters to avoid this exception.

May throw InvalidAddress. May throw InvalidMessageContents. May throw InvalidSecurity. May throw KmsAccessDenied. May throw KmsDisabled. May throw KmsInvalidKeyUsage. May throw KmsInvalidState. May throw KmsNotFound. May throw KmsOptInRequired. May throw KmsThrottled. May throw QueueDoesNotExist. May throw RequestThrottled. May throw UnsupportedOperation.

Parameter messageBody : The message to send. The minimum size is one character. The maximum size is 1 MiB or 1,048,576 bytes

#x9 | #xA | #xD | #x20 to #xD7FF | #xE000 to #xFFFD | #x10000 to #x10FFFF

If a message contains characters outside the allowed set, Amazon SQS rejects the message and returns an InvalidMessageContents error. Ensure that your message body includes only valid characters to avoid this exception.

Parameter queueUrl : The URL of the Amazon SQS queue to which a message is sent.

Queue URLs and names are case-sensitive.

Parameter delaySeconds : The length of time, in seconds, for which to delay a specific message. Valid values: 0 to 900. Maximum: 15 minutes. Messages with a positive DelaySeconds value become available for processing after the delay period is finished. If you don't specify a value, the default value for the queue applies.

Parameter messageAttributes : Each message attribute consists of a Name, Type, and Value. For more information, see Amazon SQS message attributes in the Amazon SQS Developer Guide.

Parameter messageDeduplicationId : This parameter applies only to FIFO (first-in-first-out) queues.

The token used for deduplication of sent messages. If a message with a particular MessageDeduplicationId is sent successfully, any messages sent with the same MessageDeduplicationId are accepted successfully but aren't delivered during the 5-minute deduplication interval. For more information, see Exactly-once processing in the Amazon SQS Developer Guide.

  • Every message must have a unique MessageDeduplicationId,
    • You may provide a MessageDeduplicationId explicitly.
    • If you aren't able to provide a MessageDeduplicationId and you enable ContentBasedDeduplication for your queue, Amazon SQS uses a SHA-256 hash to generate the MessageDeduplicationId using the body of the message (but not the attributes of the message).
    • If you don't provide a MessageDeduplicationId and the queue doesn't have ContentBasedDeduplication set, the action fails with an error.
    • If the queue has ContentBasedDeduplication set, your MessageDeduplicationId overrides the generated one.
  • When ContentBasedDeduplication is in effect, messages with identical content sent within the deduplication interval are treated as duplicates and only one copy of the message is delivered.
  • If you send one message with ContentBasedDeduplication enabled and then another message with a MessageDeduplicationId that is the same as the one generated for the first MessageDeduplicationId, the two messages are treated as duplicates and only one copy of the message is delivered.

Implementation

Future<SendMessageResult> sendMessage({
  required String messageBody,
  required String queueUrl,
  int? delaySeconds,
  Map<String, MessageAttributeValue>? messageAttributes,
  String? messageDeduplicationId,
  String? messageGroupId,
  Map<MessageSystemAttributeNameForSends, MessageSystemAttributeValue>?
      messageSystemAttributes,
}) async {
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.0',
    'X-Amz-Target': 'AmazonSQS.SendMessage'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'MessageBody': messageBody,
      'QueueUrl': queueUrl,
      if (delaySeconds != null) 'DelaySeconds': delaySeconds,
      if (messageAttributes != null) 'MessageAttributes': messageAttributes,
      if (messageDeduplicationId != null)
        'MessageDeduplicationId': messageDeduplicationId,
      if (messageGroupId != null) 'MessageGroupId': messageGroupId,
      if (messageSystemAttributes != null)
        'MessageSystemAttributes':
            messageSystemAttributes.map((k, e) => MapEntry(k.value, e)),
    },
  );

  return SendMessageResult.fromJson(jsonResponse.body);
}