Mqtt.js: how would I send Json ?

Created on 26 Nov 2016  路  3Comments  路  Source: mqttjs/MQTT.js

I tried to send the json message but failed

    client.publish(  "lights/hue/device123/set/led",  {'a':5}, { retain: true },function(err){
        if(err){
            console.log('publishing message',failed);
        }
    });

I know it does't support json, but would be a better way to do it

Most helpful comment

client.publish('lights/hue/device123/set/led', JSON.stringify({ a: 5 }))

All 3 comments

client.publish('lights/hue/device123/set/led', JSON.stringify({ a: 5 }))

Got it, thanks @ralphtheninja

You need to do the opposite in .on('message', fn), e.g.

client.on('message', function (topic, payload) {
  const obj = JSON.parse(payload.toString()) // payload is a buffer
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jonhardman picture jonhardman  路  3Comments

bicccio picture bicccio  路  3Comments

goliatone picture goliatone  路  4Comments

jeffdare picture jeffdare  路  9Comments

sublimator picture sublimator  路  8Comments