Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Node.js MQTT example

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:

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:
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:

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


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
After some troubleshooting, the problem ended up being with the section of this section of the code.

count+=1; //quit script after the execution of two loops
if (count==2) //ens script
clearTimeout(timer_id); //stop timer
client.end();

Once I removed this section, I was able to receive messages on the console. They show up with the expected format of "message is x" "topic is y" I am a little confused as to why this section was stopping everything though
 
Back
Top Bottom