# How to send messages to a broker via mqtt

**URL:** https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651
**Category:** General
**Created:** [24 April 2021 09:55 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651 "2021-04-24T09:55:48Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 09:55 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/1 "2021-04-24T09:55:49Z")

</div>

Hi everyone, it is the first time I am using Node Red and I have a question to you.  
I'm trying to send my data to a broker via mqtt and I want to specify the message in the "function" block. I've already set the broker adress on the mqtt out node. What should I write on the function node to pass a message directly to the mqtt out using msg.topic?  
Thank you all

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 10:16 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/2 "2021-04-24T10:16:11Z")

</div>

You have to specify the mqtt topic in either the `mqtt-out` node or pass it into the `mqtt-out` node in msg.topic. The data being sent should be in msg.payload.

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 10:30 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/3 "2021-04-24T10:30:00Z")

</div>

Ok, the broker I'm using is thingspeak so in the topic of the mqtt-out I have to put the channel where i want to publish the data with the correct structure and then for example "/msg.payload.temp" if my data is a temperature?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [24 April 2021 10:38 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/4 "2021-04-24T10:38:21Z")

</div>

Give us an actual example with real numbers. Tell us what is in the example incoming message and tell us what MQTT topic and value you want published.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 10:56 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/5 "2021-04-24T10:56:36Z")

</div>

From the `Help` tab of the `mqtt-out` node:

### [Details](http://127.0.0.1:1880/#)

`msg.payload` is used as the payload of the published message. If it contains an Object it will be converted to a JSON string before being sent. If it contains a binary Buffer the message will be published as-is.

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 11:44 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/6 "2021-04-24T11:44:57Z")

</div>

I have some weather data in Json, and I want to pass these data to my thingspeak channel using MQTT

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 11:46 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/7 "2021-04-24T11:46:33Z")

</div>

Ok thanks, but can you give me a guide of how write messages in the function block? I am not very practical

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 12:19 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/8 "2021-04-24T12:19:11Z")

</div>

Why do you need to use a function node?  
What does the data coming if into your flow look like and what does it need to look like going into the MQTT-out node?

Have you read the documentation about functions?

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 14:54 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/9 "2021-04-24T14:54:41Z")

</div>

Because I am trying to follow this sentence on the mqtt out node: Leave topic blank if you want to set it via msg properties". So I do not know where to set it, i tought it was something on the function node...

---

<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: [24 April 2021 14:58 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/10 "2021-04-24T14:58:49Z")

</div>

You can set msg.topic in a function node or a change node.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 15:00 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/11 "2021-04-24T15:00:22Z")

</div>

Or in the MQTT-out node. You have a choice and can pick the one best suited to what you want to do.

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 15:37 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/12 "2021-04-24T15:37:23Z")

</div>

ok, so in the function node i can write something like "msg.topic=msg.payload..."? And i have to return msg.topic?

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 15:38 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/13 "2021-04-24T15:38:03Z")

</div>

In the Topic field? Because I have tried many times to set it in the MQTT node

---

<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: [24 April 2021 15:48 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/14 "2021-04-24T15:48:11Z")

</div>

You must return a msg object from a function.

What are you actually trying to achieve? Why would you want to set topic to value of payload?

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 15:59 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/15 "2021-04-24T15:59:46Z")

</div>

Because I didn't manage to set it by using the mqtt-out "topic" field. I want to send 2 different data in json to a broker

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 16:36 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/16 "2021-04-24T16:36:30Z")

</div>

Fred, a MQTT message has two parts, the topic and the payload. The topic is what another device will subscribe to.

Let's say you have device A that is reading a BME280 and `publishing` the temperature, humidity and pressure. It would put all three in the payload and then set a topic like `node34/livingroom/data` and then publish that message. The broker (a device in control of all the messages) when it receives the message will look to see who has subscribed to that topic (there are wildcard's thattt can be used in the topic but ignore them for now).

Now say device X and device M have both subscribed to that topic, then the broker will send the message to both and each one will receive the message with the topic and the payload.  
(you shoud read [MQTT Essentials - All Core Concepts explained](https://www.hivemq.com/mqtt-essentials/) to get an understanding of MQTT)

So in your case, you can set the topic in the `mqtt-out` node (and it will be fixed) or you can set it in the msg being sent to the `mqtt-out` node in msg.topic with the data in msg.payload.

Sending it via msg.topic allows you to publish to multiple topics and only need one `mqtt-out` node. If you hard code in in the `mqtt-out` node, then you will ned one node for every topic you publish to.

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 17:24 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/17 "2021-04-24T17:24:02Z")

</div>

Ok thank you. So I have to write two parts: one is the channel where I am publishing it (e.g. the channel of thingspeak I created), the second one is the payload. the thing I do not understeand is how i can write the payload part...

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 17:36 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/18 "2021-04-24T17:36:23Z")

</div>

I try to be more specific: I have set my channel and all the stuff and the broker tells me that I am publishing something (entries != 0) indeed. So I think the problem is the way/format I am publishing data

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 April 2021 17:44 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/19 "2021-04-24T17:44:51Z")

</div>

Do this, add an ` mqtt-in` node connected to a ` debug` node (set to display the complete msg object) and set the mqtt topic in the node to the one you are using to publish the message. Then you will see that the msg is being sent.

---

<div class="post-metadata">

### Author: ![fred1985](https://avatars.discourse-cdn.com/v4/letter/f/41988e/32.png) [@fred1985](https://discourse.nodered.org/u/fred1985)
#### Post date: [24 April 2021 17:53 UTC](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651/20 "2021-04-24T17:53:03Z")

</div>

I still can't see anything on my broker as a subscriber...

[Next page](https://discourse.nodered.org/t/how-to-send-messages-to-a-broker-via-mqtt/44651.md?page=2)
