0

Right so I am trying to transfer some files using mqtt in nodejs. This works fine if the file are tiny, but something as small a 6 mb rar file becomes 21 MB when it is turned into a string or buffer.

I need to have some details with the file like path and filename, so I have created an object that contains, the file buffer and the data I need. Once I have that, I Stringify it with JSON and can send it.

What is the best way to make sure that you get proper buffer size for mqtt transfer?

Thanks in advance. Thomas

var mqtt = require('mqtt');

var fs = require('fs');
let client
let message
let bufferMessage
let data

function genPayload() {
  data = fs.readFileSync('app.rar');


  let message = {
    "filename": "app.rar",
    "filePath": "C:\\test\\",
    "data": data
  }

  console.log('Preparing File')
  bufferMessage = JSON.stringify(message);

}

function Connect() {
  client = mqtt.connect("mqtt://test.mosquitto.org", {
    clientId: "vfs001"
  });

  client.on('connect', function () {
    console.log('Client Connected')
  });
}

function SendFile() {
  client.publish('TestReplFile', bufferMessage)
  console.log('File is on the way')

};

genPayload();
Connect();
setTimeout(SendFile, 5000);

1 Answer 1

4

The problem is that fs.readfileSync() will return a Buffer and when you stringify that it will generate somethings that looks like:

...
data: [ 0x11, 0x44, 0xcf, ... ]
...

Which leads to at least 4 characters for every byte in the file.

To get the smallest JSON safe representation of a binary file you probably want to base64 encode it.

function genPayload() {
  data = fs.readFileSync('app.rar');

  let message = {
    "filename": "app.rar",
    "filePath": "C:\\test\\",
    "data": data.toString('base64')
  }

  console.log('Preparing File')
  bufferMessage = JSON.stringify(message);

}

Base64 will still be about 30% bigger than the input file, but it should still be smaller that a straight stringified buffer.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a life saver @hardillb, it is now working exactly like i would like to have it work. Thank you very much for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.