Javascript訂閱、發佈MQTT訊息則是使用MQTT.js。
安裝
npm install mqtt --save
發佈
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://127.0.0.1:1883')
client.on('connect', function () {
client.publish('temperature/Wuling', '23.4');
client.end();
})
訂閱
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://127.0.0.1:1883')
client.on('connect', function () {
client.subscribe('temperature/Wuling', function (err) {
})
})
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString())
})