Last active
April 23, 2021 05:27
-
-
Save yoonseopshin/186c7ca0287c580643b11acce550501d to your computer and use it in GitHub Desktop.
MQTT File Copy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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