How to set ResponseTopic in mqtt request message

Hello All,

We want to pass response topic as a part of MQTT message. How can we pass response topic along with message. We tried with function node by setting up property "ResponseTopic" but it doesn't seems to be working.

Thanks in advance.

Regards,
Tejas

Are you refering to the MQTT V5 response topic?

Are you using an MQTT V5 broker?

Here is a working demo using an external broker

publish to cloud broker on "demo/request" and a responseTopic of "demo/response"

when you get the published value for "demo/request" you set/adjust the payload with your "answer" then send the result to the topic specified in responseTopic (which in this case is "demo/response")

Then at step 4 - you see we are subscribed to demo/response & we get the "answer" delivered.

[{"id":"28f0a45c19686fd5","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"[99,111,114,114,101,108,45,100,97,116,97]","vt":"bin"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number1\":1,\"subobj\":{\"sub1\":123}}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2420,"y":820,"wires":[["19787fc720cb68ab"]]},{"id":"19787fc720cb68ab","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"demo/request","qos":"","retain":"","respTopic":"demo/response","contentType":"text/plain","userProps":"","correl":"","expiry":"","broker":"dc811dae.05f5","x":2580,"y":820,"wires":[]},{"id":"7bc91b8e43fc5073","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/request","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2410,"y":940,"wires":[["68b68b0196181c93"]]},{"id":"97d93f0a378e8811","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"dc811dae.05f5","x":2910,"y":940,"wires":[]},{"id":"1e4ccf4168a1a8dd","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/response","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2760,"y":820,"wires":[["21cf9bced1749aec"]]},{"id":"21cf9bced1749aec","type":"debug","z":"4b3f21a3.ba434","name":"req res pattern","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2940,"y":820,"wires":[]},{"id":"68b68b0196181c93","type":"change","z":"4b3f21a3.ba434","name":"delete topic & add random payload","rules":[{"t":"delete","p":"topic","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"$floor( ($random() * 10) + 1)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2660,"y":940,"wires":[["97d93f0a378e8811"]]},{"id":"b3fec62de35fb3c9","type":"comment","z":"4b3f21a3.ba434","name":"*************** Response topic demo ************************","info":"","x":2550,"y":680,"wires":[]},{"id":"4b105356a7e74661","type":"comment","z":"4b3f21a3.ba434","name":"send payload to \"demo/request\" expect reply on \"demo/response\"","info":"","x":2570,"y":720,"wires":[]},{"id":"4093181e7772c1ad","type":"comment","z":"4b3f21a3.ba434","name":"fake external application recieving command and responding to reply topic","info":"","x":2600,"y":900,"wires":[]},{"id":"44fe4ed1ec155c5f","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"correl sent in msg as a string","vt":"str"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number\":1}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2420,"y":860,"wires":[["19787fc720cb68ab"]]},{"id":"8b570801642403a4","type":"inject","z":"4b3f21a3.ba434","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"correl sent in msg as a string","vt":"str"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number2\":2, \"number1\":1}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2420,"y":780,"wires":[["19787fc720cb68ab"]]},{"id":"dc811dae.05f5","type":"mqtt-broker","name":"","broker":"broker.emqx.io","port":"1883","clientid":"","usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"50","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Here is a more dynamic example where the responseTopic is set progromatically (in the inject node) and passed into the MQTT node.

image

[{"id":"28f0a45c19686fd5","type":"inject","z":"4b3f21a3.ba434","name":"demo/response2","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"[99,111,114,114,101,108,45,100,97,116,97]","vt":"bin"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number1\":1,\"subobj\":{\"sub1\":123}}","vt":"json"},{"p":"responseTopic","v":"demo/response2","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2380,"y":820,"wires":[["19787fc720cb68ab"]]},{"id":"19787fc720cb68ab","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"demo/request","qos":"","retain":"","respTopic":"","contentType":"text/plain","userProps":"","correl":"","expiry":"","broker":"dc811dae.05f5","x":2580,"y":820,"wires":[]},{"id":"7bc91b8e43fc5073","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/request","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2350,"y":980,"wires":[["68b68b0196181c93"]]},{"id":"97d93f0a378e8811","type":"mqtt out","z":"4b3f21a3.ba434","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"dc811dae.05f5","x":2910,"y":980,"wires":[]},{"id":"1e4ccf4168a1a8dd","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/response1","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2760,"y":760,"wires":[["21cf9bced1749aec"]]},{"id":"21cf9bced1749aec","type":"debug","z":"4b3f21a3.ba434","name":"req res pattern","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2940,"y":760,"wires":[]},{"id":"68b68b0196181c93","type":"change","z":"4b3f21a3.ba434","name":"delete topic & add random payload","rules":[{"t":"delete","p":"topic","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"$floor( ($random() * 10) + 1)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2640,"y":980,"wires":[["97d93f0a378e8811"]]},{"id":"b3fec62de35fb3c9","type":"comment","z":"4b3f21a3.ba434","name":"*************** Response topic demo ************************","info":"","x":2490,"y":680,"wires":[]},{"id":"4b105356a7e74661","type":"comment","z":"4b3f21a3.ba434","name":"send payload to \"demo/request\" expect reply on the responseTopic set in the inject node","info":"","x":2580,"y":720,"wires":[]},{"id":"4093181e7772c1ad","type":"comment","z":"4b3f21a3.ba434","name":"fake external application recieving command and responding to reply topic","info":"","x":2540,"y":940,"wires":[]},{"id":"44fe4ed1ec155c5f","type":"inject","z":"4b3f21a3.ba434","name":"demo/response3","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"correl sent in msg as a string","vt":"str"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number\":1}","vt":"json"},{"p":"responseTopic","v":"demo/response3","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2380,"y":860,"wires":[["19787fc720cb68ab"]]},{"id":"8b570801642403a4","type":"inject","z":"4b3f21a3.ba434","name":"demo/response1","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"correlationData","v":"correl sent in msg as a string","vt":"str"},{"p":"aa","v":"123","vt":"num"},{"p":"userProperties","v":"{\"number2\":2, \"number1\":1}","vt":"json"},{"p":"responseTopic","v":"demo/response1","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":2380,"y":780,"wires":[["19787fc720cb68ab"]]},{"id":"4fb4c56e810f05d2","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/response2","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2760,"y":820,"wires":[["fc3420b94da5a25d"]]},{"id":"fc3420b94da5a25d","type":"debug","z":"4b3f21a3.ba434","name":"req res pattern","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2940,"y":820,"wires":[]},{"id":"7b2c384ce917327f","type":"mqtt in","z":"4b3f21a3.ba434","name":"","topic":"demo/response3","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":false,"x":2760,"y":880,"wires":[["7cf85a9adfa8092a"]]},{"id":"7cf85a9adfa8092a","type":"debug","z":"4b3f21a3.ba434","name":"req res pattern","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2940,"y":880,"wires":[]},{"id":"dc811dae.05f5","type":"mqtt-broker","name":"","broker":"broker.emqx.io","port":"1883","clientid":"","usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"50","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

The settable inputs are listed in the built in help...

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.