NodeJS - 订阅所有 MQTT 主题

NodeJS - Subscribe to all MQTT Topics

我正在使用 NodeJS 的 mqtt 模块。一切正常,即发布、订阅特定主题、处理连接错误等。问题是当我尝试订阅所有主题时,通常以 client.subscribe('#',callback) 完成,它不订阅任何主题。

这是相关代码:

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

订阅所有主题的代码:

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

这是 listenIncoming() 函数,它打印出收到的消息:

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 '#'
    })
}

如何订阅所有主题?我使用的代理是 hivemq (mqtt://broker.hivemq.com)

不允许在 broker.hivemq.com 上订阅 #

正如使用 mosquitto_sub

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

或者您的代码显示的 qos 为 128(来自 spec)。

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