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)