MQTT Dynamic topic and publish the messages

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;

msg.payload is an array
so

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

should work.

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

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.