Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send JSON message to Azure Queue storage

I am using azure-storage node module. I would like to send JSON on my queue and get it into my queue on azure function.

I send message in queue. I stringify my message to put in queue.

// This is my service who send message to queue via node lib azure-storage
       const queueMsg = {
              userId,
              token: tokenNotif
            };
            queueSvc.createMessage(Config.REGISTRATION_FCM_PUSH_NOTIFICATION_QUEUE, JSON.stringify(queueMsg), (err) => {
              if (!error) {
                this._logger.info(`User ${userId} has register this push notification token`);
                resolve(true);

              } else {
                reject(false);
              }
            });

And in the queue function i have an error because the function think isn't a string and push the message text on xx-queue-poison {"userId":"a6c8a103-dacc-4b15-bffd-60693105f131","token":"xxxx"}

I don't know why the quote is replaced by ASCII code on queue ?

I have tested something else! From my service i call a Http Azure function, and this function call the Queue Storage and it's work by this way :s ..

The HttpTrigger function call queue context.bindings.notificationQueue = [{ userId: name, token }];

And queue received data context.log(`Received userId ${queueItem.userId} :: ${queueItem.token}`);

Why by using HttpTrigger function to QueueTrigger function it's working, but when i am using the lib "azure-storage" is not working ?

Thx

like image 541
johan pujol Avatar asked Nov 14 '25 15:11

johan pujol


1 Answers

I don't know why the quote is replaced by ASCII code on queue ?

Basically the SDK is converting the string message to make it XML safe. If you look at the code in the SDK, by default it uses TextXmlQueueMessageEncoder as the message encoder. The encode function replaces " with " to make it XML safe.

From the SDK code (partial code snippets):

QueueService.prototype.createMessage = function (queue, messageText, optionsOrCallback, callback) {
  var userOptions;
  azureutil.normalizeArgs(optionsOrCallback, callback, function (o, c) { userOptions = o; callback = c; });

  validate.validateArgs('createMessage', function (v) {
    v.string(queue, 'queue');
    v.queueNameIsValid(queue);
    v.callback(callback);
  });

  var xmlMessageDescriptor = QueueMessageResult.serialize(messageText, this.messageEncoder);

function TextXmlQueueMessageEncoder(){
}
util.inherits(TextXmlQueueMessageEncoder, QueueMessageEncoder);

/**
 * Encode utf-8 string by escaping the xml markup characters.
 * @this TextXmlQueueMessageEncoder
 * 
 * @param   {string}    [input]               The target to be encoded.
 * 
 * @return {string}
 */
TextXmlQueueMessageEncoder.prototype.encode = function(input){
  return input.replace(/&/gm, '&')
    .replace(/</gm, '&lt;')
    .replace(/>/gm, '&gt;')
    .replace(/"/gm, '&quot;')
    .replace(/'/gm, '&apos;');
};

One possible solution would be to convert the string to a base64 encoded string as you suggested. However if you're using the SDK to retrieve the messages, you should not see these &quot; in your message body as the SDK takes care of decoding the message.

like image 55
Gaurav Mantri Avatar answered Nov 17 '25 09:11

Gaurav Mantri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!