MQTT Additional Header/Payload

Hi guys,

First time working with MQTT and I'm excited how easy it was to setup the broker and configure 2 Clients in Node-Red to connect.

First a general question: Is it possible to send more than the "payload" via MQTT e.g. an additional Header?

In my case I receive an HTTP Call with a payload , which I send to the MQTT Broker. But I also need one of the objects from msg.res.xxx.xxx (doesn't really matter) to be send in the same call to the broker.
I tried to just add the field to the payload via a function node, but on the subscribed client side just the "original" payload is received.

Function node:
var payload=msg.payload; //get payload
var parsedUrl=msg.req._parsedUrl.search;
msg.payload.parsedUrl=parsedUrl;
return msg;

Flow:
image

Any idea?


image

Hi again,

It seems that I can't change the payload due to the format? As changing the topic or adding another field is working fine.
So rephrasing my question a little bit:
Can I add an additional object send via MQTT. Something like "msg.header" or "msg.payload2" :smiley:

Or how to change the payload format? Trying to convert it to JSON String gives me the error "Ignored non-object payload"

Thanks in advance for your help.

MQTT messages are generally a string sent to a specific topic. But you can send a json formatted object via mqtt and encode or decode it with the help of the json node.

No, but whatever you put in msg.payload should be sent to mqtt. Try this in your function node

msg.payload = {response: msg.payload, url: msg.req._parsedUrl.search
return msg;

Or you can do the same thing in a Change node.
However, I don't see msg.req._parsedURL.search in your debug output, are you sure you don't mean msg.req.url?

Thanks for your reply.
Yes, the msg.req._parsedUrl.search was just not on the screenshot.

If I use your code in the function node, no msg is getting out of the node. (checked with a debug node)

msg.payload = {response: msg.payload, url: msg.req._parsedUrl.search}
return msg;

UPDATE: Sorry, my mistake. Works! :slight_smile: