Thursday, 11 May 2017

Spot empty/null mqtt messages

I am working with MQTT mosquitto broker and mqtt.js (from npm). Some clients send retain message like

client.publish('client1Topic',JSON.Stringify({Some:informations}),{retain: true})

Once the client is disconnected, it send a last will with retain : true. I have a central client who handles the informations from the last wills and then erase those last wills. To do so, my central client does:

client.on('message',(message,topic) => {
    doSomeStuffWithTheLastWill(JSON.Parse(message))
    client.publish(topic,null, {retain: true})
})

This null message triggers a JSON parse for the client listening on this channel. To avoid the unhandled Promised raised by JSON.Parse, I do

try{
  JSON.Parse(message) 
} catch (e) { console.log(e) }

I would like to do something like

if(message.isNull()){ return }
else {
    const parsedMessage = JSON.Parse(message)
    doSomeStuff(parsedMessage)
}

What is the good test to find wether a mqtt message's payload is null?

I've tried message ===null, payload === null, message === '' but none of those test was valide for a null message.



via Théophile Pace

No comments:

Post a Comment