简体   繁体   中英

MQTT Node.js script not receiving subscribed topic information

I recently saw a post which had some code I wanted to try. But am noticing an error which I can't debug. link The error is the code doesn't receive messages from topics it subscribes to.

The adapted code looks like this:

//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("/path/ca.crt");
var topic = "beta";
var message = "testing";
var message1 = "testing1";
var message2 = "testing2";
var message3 = "testing3";

var options={
    //host:'brokerip',
    port:8883,
    clientId:"alpha",
    username:"user",
    password:"pswd",
    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);
    client.subscribe(topic, {qos:1});
})

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

But I am noticing that there isn't any feedback from the subscribed topic of "beta" I should see "message is test3" "topic is beta" once I post something to the topic to which I also subscribe. This small section of the code takes care of this:

//handle incoming messages
client.on('message',function(topic, message, packet){
    console.log("message is "+ message);
    console.log("topic is "+ topic);
});

Instead the only feedback I receive is:

# node websitemqttclientx2.js
subscribing to topics
connected  true
publishing testing3
publishing testing3

Here is a view of my mosquitto mqtt broker's ACL file:

# Allow anonymous access to the sys
topic read $SYS/#

# Allow anonymous to read topic
topic read beta/#

# readwrite to the loop
user myusername
topic beta/#

For the following test I removed the ACL file , which should give me the ability to post and subscribe to any topic. I opened two terminals on my broker server one as a subscriber and one as a publisher. For the subscriber I executed the code below and the cursor moved to a new empty line to listen for incoming messages.

mosquitto_sub -h localhost -t beta -u myuser -P mypwd

On my publishing terminal window I entered:

mosquitto_pub -h local host -u myuser -P mypwd -t beta -m "hello world" -d --cafile ca.crt -p 8883 --insecure

and received the message below on the publisher terminal window but nothing on the subscriber window. Why I do not know.

Client mosq-xxxxxxx sending CONNECT
Client mosq-xxxxxxx received CONNACK (0)
Client mosq-xxxxxxx sending PUBLISH (d0, q0, r0, m1. 'beta' ... (11 bytes))
Client mosq-xxxxxxx sending DISCONNECT

The code you've posted is not complete (missing closing } from your publish function) and you haven't shown were you are calling that function.

You also haven't followed the instructions in the answer to the question you took the code from, which clearly states that you should be calling client.subscribe(topic) in the on connect callback.

client.on("connect",function(){
    console.log("connected  "+ client.connected);
    client.subscribe(topic, {qos: 1})
})

The Mosquitto sub/pub can be used for an initial validation. And I was able to verify the code you posted works. But along with deleting the ACL file you will have to remove references to it from your.conf file. This is the step that you are missing.

Subscriber:

mosquitto_sub -h localhost -t beta -u myuser -P mypwd

Publisher:

mosquitto_pub -h local host -u myuser -P mypwd -t beta -m "hello world" -d --cafile ca.crt -p 8883 --insecure

After you have deleted ACL references in the.conf and verified the local publishing/subscribing go ahead and run the main js it should be able to publish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM