# MQTT Dynamic topic and publish the messages

**URL:** <https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702>\
**Category:** General\
**Created:** [25 October 2021 11:30 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702 "2021-10-25T11:30:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![samynathan17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/samynathan17/32/49923_2.png) [@samynathan17](https://discourse.nodered.org/u/samynathan17)\
**Post date:** [25 October 2021 11:30 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702/1 "2021-10-25T11:30:01Z")

</div>

I'm retrieving results from the Postgres database and my result set has a topic name, based on the topic name how do I will publish the messages to the MQTT publisher node.?

Input :  
inject node = select id,COALESCE(name,default\_topic) as topic,planid,status from public.topic

Postgres resultSet:  
{  
"id": 1  
"topic": "mqtt\_test1",  
"planid": 7,  
"status": "1"}

Function node :

msg.topic = msg.payload.topic;  
msg.payload = msg.payload;  
return msg;

 ![node-red mqtt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fbad3346ff4b9822b8d5307dc7d92d470a0849ff.png)

---

<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:** [25 October 2021 11:34 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702/2 "2021-10-25T11:34:39Z")

</div>

msg.payload is an array  
so

```auto
msg.topic = msg.payload[0].topic;
msg.payload = msg.payload[0];

```

should work.

---

<div class="post-metadata">

**Author:** ![samynathan17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/samynathan17/32/49923_2.png) [@samynathan17](https://discourse.nodered.org/u/samynathan17)\
**Post date:** [25 October 2021 13:17 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702/3 "2021-10-25T13:17:05Z")

</div>

Thanks, it's working well .

And I have another question:

Node-red MQTT-publisher side publish the messages and subscriber side I'm creating HTML page their I need to consume the messages

MQTT subscriber part not working in browser-side js port number 8883 is not connecting . 8884 is connecting but node-red MQTT-publisher node side 8884 not working

---

<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:** [25 October 2021 14:40 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702/4 "2021-10-25T14:40:50Z")

</div>

> [@samynathan17](#):
>
> MQTT subscriber part not working in browser-side js port number 8883 is not connecting

Stick to port 8883 (assuming you are using secure MQTT).

1. Where are the publisher, subscriber and broker?
2. What hardware and OS for them?
3. What broker are you using?
4. If you connect using MQTT Explorer (or similar tool) are you able to connect, publish and subscribe? Including seeing values published by node-red
5. Can you explain what you mean by 'browser side' js? Are you trying to connect to MQTT from within the browser?
6. Show us how you have configured the node-red publisher.

---

<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:** [24 December 2021 14:41 UTC](https://discourse.nodered.org/t/mqtt-dynamic-topic-and-publish-the-messages/52702/5 "2021-12-24T14:41:11Z")

</div>

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