Soham
Soham

Reputation: 4417

Get particular message from Amazon SQS

Is there any way to retrieve message by some id. In this answer it is written that it's not possible. But as the answer is old so I am asking again if it's still the same or not.

I am sending message in the below way --

const params = {
      DelaySeconds: 0,
      MessageAttributes: {
        test: {
          DataType: 'String',
          StringValue: 'bdbdh',
        },
      },
      MessageBody: JSON.stringify({
        AccountId: '100'
      }),
      QueueUrl: 'url',
    };

    return new Promise((resolve, reject) => {
      sqs.sendMessage(params, function(err, data) {
        if (err) {
          console.log('data', err);
          reject(err);
        } else {
          console.log('data', data);
          resolve(data);
        }
      });
    });

Retrieving the message in the below way --

const params = {
      MaxNumberOfMessages: 10,
      MessageAttributeNames: ["test"],
      VisibilityTimeout: 600,
      QueueUrl: 'url',
    };

    return new Promise((resolve, reject) => {
      sqs.receiveMessage(params, function(err, data) {
        if (err) {
          console.log('data', err);
          reject(err);
        } else {
          console.log('data', data);
          resolve(data);
        }
      });
    });

I has also tried to get the messages by attribute name,but no luck.

Upvotes: 0

Views: 2221

Answers (2)

John Rotenstein
John Rotenstein

Reputation: 270089

No. It is not possible to retrieve a specific message from an Amazon SQS queue. You can call ReceiveMessage() to get 1 to 10 messages, but you cannot choose which messages to receive.

You can add message attributes to a message (eg priority, customer number) but they can't be used to retrieve a specific or subset of messages.

In general, messages come back in order but this is not guaranteed. For example, a message that was invisible and then made visible again will be out-of-order. Also, message order is impacted by the distributed nature of the servers used by Amazon SQS.

See: Amazon SQS short and long polling - Amazon Simple Queue Service

Message order is guaranteed for a first-in-first-out (FIFO) queue, but it cannot let you access a specific message.

Upvotes: 2

Alboman
Alboman

Reputation: 325

Unfortunately, I don't think there is a way to pull from sqs by id.

Per SQS documentation there is no parameter that you can pass:

var params = {
  QueueUrl: 'STRING_VALUE', /* required */
  AttributeNames: [
    All | Policy | VisibilityTimeout | MaximumMessageSize | MessageRetentionPeriod | ApproximateNumberOfMessages | ApproximateNumberOfMessagesNotVisible | CreatedTimestamp | LastModifiedTimestamp | QueueArn | ApproximateNumberOfMessagesDelayed | DelaySeconds | ReceiveMessageWaitTimeSeconds | RedrivePolicy | FifoQueue | ContentBasedDeduplication | KmsMasterKeyId | KmsDataKeyReusePeriodSeconds,
    /* more items */
  ],
  MaxNumberOfMessages: 'NUMBER_VALUE',
  MessageAttributeNames: [
    'STRING_VALUE',
    /* more items */
  ],
  ReceiveRequestAttemptId: 'STRING_VALUE',
  VisibilityTimeout: 'NUMBER_VALUE',
  WaitTimeSeconds: 'NUMBER_VALUE'
};
sqs.receiveMessage(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

AWS SQS docs

Upvotes: 0

Related Questions