From 24d94053da98113ad6e2a84f5281da00414ab300 Mon Sep 17 00:00:00 2001 From: shubhankar Date: Fri, 1 Sep 2017 14:15:41 +0000 Subject: [PATCH 1/2] added method to push message with attributes --- index.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/index.js b/index.js index 4e9f23f..73600ea 100644 --- a/index.js +++ b/index.js @@ -129,6 +129,33 @@ module.exports = function(options) { }); }; + that.pushWithAttributes = function(name, message, attributes, callback) { + //attributes like {"category":"book","bookid":5492} + + message = options.raw ? message : JSON.stringify(message); + + name = namespace + name; + var payload = { + MessageBody: message + }; + + var attributeNames = Object.keys(attributes); + + //sqs accepts only 10 attributes + attributeNames.splice(10); + + attributeNames.forEach(function(attributeName, index) { + var baseName = "MessageAttribute." + (index + 1); + payload[baseName + ".Name"] = attributeName; + payload[baseName + ".Value.StringValue"] = attributes[attributeName]; + payload[baseName + ".Value.DataType"] = typeof attributes[attributeName] == "number" ? "Number" : "String"; + }) + + queueURL(name, function(url) { + retry(request, queryURL('SendMessage', url, payload), callback); + }); + }; + that.delete = that.del = function(name, callback) { name = namespace+name; From a7b0b5e7079a9851bae60f9beaf532a449ca40da Mon Sep 17 00:00:00 2001 From: Shubhankar Date: Fri, 1 Sep 2017 15:17:01 +0000 Subject: [PATCH 2/2] Update README.md Added documentation for pushWithAttributes method --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 987b9f2..699a97a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,20 @@ queue.pull('test', function(message, callback) { callback(); // we are done with this message - pull a new one // calling the callback will also delete the message from the queue }); +``` +Use the following method add [SQS Message Attributes](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-attributes.html) to the message +```js +// push data to the 'test' queue with the message body as {some:"data"} +// The message has 2 message attributes as {"category":"book","bookid":5492} +queue.pushWithAttributes('test', { + some:'data' +}, { + category: "book", + bookid: 5492 +}, function() { + //complete +}); + ``` ## API