Feynman137
Coder
I have a script that I've been trying to figure out. It is a test script for a client to publish messages to an MQTT broker server under a certain topic, which the client then subscribes to so it can confirm the broker server got processed its message. The main code is posted at the very bottom but I am getting a little response from the code, which looks like:
Specifically, because I am publishing messages to the "weather" topic, which I am also subscribed to I should be seeing "message is test3" "topic is weather" according to this code snippet:
Main Code
Bash:
# node websitemqttclientx2.js
subscribing to topics
connected true
publishing testing3
publishing testing3
Specifically, because I am publishing messages to the "weather" topic, which I am also subscribed to I should be seeing "message is test3" "topic is weather" according to this code snippet:
JavaScript:
//handle incoming messages
client.on('message',function(topic, message, packet){
console.log("message is "+ message);
console.log("topic is "+ topic);
});
Main Code
JavaScript:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//setup
var mqtt = require('mqtt'); //for client use
const fs = require('fs');
var count =0; //connected to an end script function
var caFile = fs.readFileSync("/pathway/ca.crt");
var topic = "weather";
var message = "testing";
var message1 = "testing1";
var message2 = "testing2";
var message3 = "testing3";
var options={
//host:'brokerip',
port:8883,
clientId:"yo",
username:"username",
password:"password",
protocol: 'mqtts',
clean:true,
rejectUnauthorized: false,
retain:false,
ca:caFile
}
var client = mqtt.connect("http://DNSaddress",options);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//connection dialog
//handle incoming messages
client.on('message',function(topic, message, packet){
console.log("message is "+ message);
console.log("topic is "+ topic);
});
client.on("connect",function(){
console.log("connected "+ client.connected);
})
//handle errors
client.on("error",function(error){
console.log("Can't connect" + error);
process.exit(1)});
//publish
function publish(topic,message1,options){
console.log("publishing",message1);
if (client.connected === true){
client.publish(topic,message2,options);
}
count+=1; //quit script after the execution of two loops
if (count==2) //ens script
clearTimeout(timer_id); //stop timer
client.end();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
console.log("subscribing to topics");
client.subscribe(topic,{qos:1}); //single topic
var timer_id=setInterval(function(){publish(topic,message3,options);},5000);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Last edited: