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);