# Send message to another topic

**URL:** <https://discourse.nodered.org/t/send-message-to-another-topic/51170>\
**Category:** General\
**Created:** [17 September 2021 15:47 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170 "2021-09-17T15:47:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fikerplunk](https://avatars.discourse-cdn.com/v4/letter/f/e19b73/32.png) [@fikerplunk](https://discourse.nodered.org/u/fikerplunk)\
**Post date:** [17 September 2021 15:47 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170/1 "2021-09-17T15:47:11Z")

</div>

Hey guys!

I'm new to node-red so I'm not getting a solution to my problem.

So I get via mqtt a message that I need to extract some data, and then I need to send two separate messages, one stop each topic, but I'm not able to do that:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04f793b28ed7d6d8af01e3db1be3ce9d421a72bb.png)

My "Extract Info" node has the following code:

```auto
var topic = msg.topic;
var payload = msg.payload;
var product = payload.gtin;
var description=payload.items[0].name;
var price = payload.items[0].price;
var thumbnail = payload.thumbnail;
var id = payload.items[0].id;
var msgid = {payload: id};

msg.payload = 

description + "\r\n" + 
"Preço : R$ " + price + "\r\n"; 

msg.topic = "esp32/product_return/"+topic.substr(19);

return [msg,msgid];

```

I would like to send the content of msgid to a topic other than msg.topic. How can I do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [17 September 2021 16:00 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170/2 "2021-09-17T16:00:48Z")

</div>

Depends how you want to set it... but you could just do

```auto
var msgid = {payload: id , topic: "myNewTopic"};

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2021 16:25 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170/3 "2021-09-17T16:25:15Z")

</div>

You can create an array of payloads and topics and then they will be sent as two seperate mssages to the same output

```auto
 [[{"payload":1,"topic":"aaa/bbb"},
{"payload":2,"topic":"fff/ggg"}]]

```

```auto
[{"id":"f70ec784.46d18","type":"inject","z":"190c8195.72b3be","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"somegggggggggg/topic/blahblahblah","payload":"","payloadType":"date","x":480,"y":740,"wires":[["39ccfe1.914be82"]]},{"id":"39ccfe1.914be82","type":"function","z":"190c8195.72b3be","name":"","func":"var topic = msg.topic;\nvar payload = msg.payload;\nvar product = \"gggg\";\nvar description=\"fffff\";\nvar price = 123;\nvar thumbnail = \"mmmm\";\nvar id = 123;\nvar msgid = {payload: id, topic:\"anothe/topc\"};\n\nfirst = {payload: description + \"\\r\\n\" + \"Preço : R$ \" + price + \"\\r\\n\",\ntopic: \"esp32/product_return/\"+topic.substr(19)};\nmsg =[[first, msgid]]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":690,"y":740,"wires":[["6182f651.7b026","7f92c9b6.d14948"]]},{"id":"6182f651.7b026","type":"mqtt out","z":"190c8195.72b3be","name":"","topic":"","qos":"","retain":"","broker":"d675b749.04b9c8","x":840,"y":840,"wires":[]},{"id":"7f92c9b6.d14948","type":"debug","z":"190c8195.72b3be","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":740,"wires":[]},{"id":"d675b749.04b9c8","type":"mqtt-broker","name":"Localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 September 2021 16:40 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170/4 "2021-09-17T16:40:45Z")

</div>

You don't have to use two outputs from your function node. Draw two wires from the same output and in one of them insert a change node.!  
The message along each wire will have the same id. At least, it did in my experiment!

 ![Untitled 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45824096c0fca8adebe1463028ec573f7a03a5be.jpeg)

---

<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:** [1 October 2021 16:41 UTC](https://discourse.nodered.org/t/send-message-to-another-topic/51170/5 "2021-10-01T16:41:18Z")

</div>

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