# How to set ResponseTopic in mqtt request message

**URL:** <https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471>\
**Category:** General\
**Created:** [22 June 2021 10:18 UTC](https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471 "2021-06-22T10:18:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tetrived](https://avatars.discourse-cdn.com/v4/letter/t/3be4f8/32.png) [@tetrived](https://discourse.nodered.org/u/tetrived)\
**Post date:** [22 June 2021 10:18 UTC](https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471/1 "2021-06-22T10:18:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 June 2021 10:46 UTC](https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471/2 "2021-06-22T10:46:44Z")

</div>

> [@tetrived](#):
>
> We want to pass response topic as a part of MQTT message

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5c29a882fb8f95d5d48d48b9d2f016250d9139b.png)

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.

```auto
[{"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":""}]

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [22 June 2021 10:52 UTC](https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471/3 "2021-06-22T10:52:40Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cde596a215574149bb61a5330c05fbb5060e462a.png)

```auto
[{"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...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e6e374b97eb854ac49c21db76e282529becbb9d.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 August 2021 10:53 UTC](https://discourse.nodered.org/t/how-to-set-responsetopic-in-mqtt-request-message/47471/4 "2021-08-21T10:53:34Z")

</div>

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