Search code examples
javascriptnode.jstypescriptamazon-sqs

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.


Solution

  • 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.