Skip to content

Instantly share code, notes, and snippets.

@yoonseopshin
Last active April 23, 2021 05:27
Show Gist options
  • Save yoonseopshin/186c7ca0287c580643b11acce550501d to your computer and use it in GitHub Desktop.
Save yoonseopshin/186c7ca0287c580643b11acce550501d to your computer and use it in GitHub Desktop.
MQTT File Copy
const mqtt = require("mqtt");
const fs = require("fs");
const client = mqtt.connect("mqtt://localhost");
client.on("connect", function () {
client.subscribe("test");
});
client.on("message", function (topic, message) {
data = JSON.parse(message);
console.log(data);
fs.writeFileSync("result-" + data.name, Buffer.from(data.data));
client.end();
});
const mqtt = require('mqtt');
const fs = require('fs');
const client = mqtt.connect('mqtt://localhost');
let fileName = "test.txt";
let data = fs.readFileSync(fileName);
let buffer = {
"name": fileName,
"data": data,
}
client.on('connect', function () {
client.subscribe('test');
client.publish('test', JSON.stringify(buffer));
})
client.on('message', function (topic, message) {
console.log(`Sender: ${topic}`);
client.end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment