0
votes

I'm using the mqtt module of NodeJS. Everything works properly, i.e publishing, subscribing to a particular topic, handling connection errors etc. The problem is when I'm trying to subscribe to all topics, which is usually done as client.subscribe('#',callback) , it doesn't subscribe to any topics.

This is the relevant code:

// Connecting to a specific topic
client = mqtt.connect(broker,options)
console.log("MQTT Connected")
client.on('connect',()=>{
    client.subscribe('mytopic/topic1',(err,granted)=>{
        if(err){
            console.log(err)
        }

        console.log(granted)
        console.log("Subscribed to mytopic/topic1")
    })
    listenIncoming()
})
client.on('error',err=>{
    console.log("ERROR in MQTT: ",err)
})

Code for Subscribing to all topics:

client = mqtt.connect(broker,options)
console.log("MQTT Connected")
client.on('connect',()=>{
    client.subscribe('#',(err,granted)=>{
        if(err){
            console.log(err)
        }

        console.log(granted)
        console.log("Subscribed to all topics")
    })
    listenIncoming()
})
client.on('error',err=>{
    console.log("ERROR in MQTT: ",err)
})

And this is the listenIncoming() function which prints out the messages as they are received:

function listenIncoming(){
    client.on('message',(topic,payload,packet)=>{
        var topics = subscribedTopics
        console.log(topics,payload) //prints it when specific topic is subscribed. Doesn't when subscribing to '#'
    })
}

How do I subscribe to all topics with this? The broker I'm using is hivemq (mqtt://broker.hivemq.com)

1

1 Answers

0
votes

Subscribing to # on broker.hivemq.com is not allowed.

As can be seen by either using mosquitto_sub

$ mosquitto_sub -v -t '#' -h broker.hivemq.com
All subscription requests were denied.

Or by the fact that your code shows a qos of 128 (from the spec).

$ node test.js 
MQTT Connected
[ { topic: '#', qos: 128 } ]
Subscribed to all topics