# MQTT publish inject credentials

**URL:** https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017
**Category:** General
**Tags:** mqtt
**Created:** [29 February 2024 09:38 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017 "2024-02-29T09:38:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)
#### Post date: [29 February 2024 09:38 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/1 "2024-02-29T09:38:01Z")

</div>

Hello,

End goal is to be able to setup "MQTT Out" credentials via ENV\_Variables

First, I'm trying to inject credentials via nodes.  
Tried to follow @Steve-Mcl guide:

> [@Dynamic MQTT node's "actions-\>connect" guidance needed](https://discourse.nodered.org/t/dynamic-mqtt-nodes-actions-connect-guidance-needed/58878/2):
>
> The dynamic connection does not overwrite fields. It simply connects to the broker specified in the msg you send. You just add a broker (with any settings you like) and set it to NOT auto connect [image] then you send the appropriate msg to set up and connect. Here is a working demo... [mqtt-dyn] [{"id":"a7f43be0a40d080b","type":"mqtt out","z":"8afdb9e0485c9da7","name":"mqtt out","topic":"","qos":"0","retain":"false","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","…

Notes I have from broker side:

- Broker setup from IT side allows nodered only to publish messages. If I try to subscribe with the same credentials, I get disconnected immediately. I delete "MQTT In" node and "MQTT Out" node recovers to "Connected" state.

- MQTT out works when the same credentials are inserted into the node statically.

As far as I understand option "Dynamic subscription" is only for "MQTT In" node and it doesn't transfer to MQTT out node.

Flow:

```auto
[{"id":"6a114e1f2c1941c4","type":"inject","z":"17544288b1b6c2c1","name":"inject","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":610,"y":2240,"wires":[["285ee8faf1b91503"]]},{"id":"285ee8faf1b91503","type":"change","z":"17544288b1b6c2c1","name":"dynamic setup","rules":[{"t":"set","p":"action","pt":"msg","to":"connect","tot":"str"},{"t":"set","p":"broker","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"broker.password","pt":"msg","to":"psw","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"data/e2e_pipeline/nodered","tot":"str"},{"t":"set","p":"broker.username","pt":"msg","to":"user","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":2240,"wires":[["79526e7c26285d53"]]},{"id":"79526e7c26285d53","type":"mqtt out","z":"17544288b1b6c2c1","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"9cdd6c4e996aa6eb","x":910,"y":2240,"wires":[]},{"id":"9cdd6c4e996aa6eb","type":"mqtt-broker","name":"test","broker":"192.168.246.2","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"6","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","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: [29 February 2024 09:48 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/2 "2024-02-29T09:48:15Z")

</div>

> [@KarolisL](#):
>
> End goal is to be able to setup "MQTT Out" credentials via ENV\_Variables

You can simply specify the credentials in the MQTT config

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/714be40c687139502170929c59f867d686d7adbf.png)

[https://nodered.org/docs/user-guide/environment-variables](https://nodered.org/docs/user-guide/environment-variables)

---

<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: [29 February 2024 09:52 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/3 "2024-02-29T09:52:10Z")

</div>

> [@KarolisL](#):
>
> As far as I understand option "Dynamic subscription" is only for "MQTT In" node and it doesn't transfer to MQTT out node

You are misunderstanding the connection between the config and the -in/-out nodes. If the -in/-out nodes have the SAME -config node selected, the "dynamic" operations are performed against the -config of that -in or -out node. Only the "subscribe" action has an effect on the -in node.

In short:

The MQTT-IN node is used in the linked demo to perform the dynamic actions (connect/disconnect/subscribe)

The MQTT-out node is still used for publishing:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d34d5df0ad2abd571f6b134423c2c766596268a3.png)

Either the -in or -out node can accept the dynamic commands - they just get pushed to the MQTT-config (and that config is shared with each -in and -out node that you have selected the -config for.

---

<div class="post-metadata">

### Author: ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)
#### Post date: [29 February 2024 10:13 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/4 "2024-02-29T10:13:01Z")

</div>

Good to know that I don't have to do anything with MQTT-IN.

I will try to check ENV\_VAR suggestions when I get access to that machine.

That said, my MQTT publish works with statically typed setup:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8ac2dff6da019fde783ccf4416ec67c06783014b.png)

When I delete credentials:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/118f62e24c47760e6326275dd262cf8899cde785.png)

It is immediately disconnected (makes sense)  
Broker setup stays the same:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/b/2bd0e14edd873decfd673ec662a2cc977b5b58b2.png)

But my inject doesn't establish connection:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/947215fc3e07142c0b47e59d4a7e08329043efb6.png)

Is there anything else I'm missing?

---

<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: [29 February 2024 10:27 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/5 "2024-02-29T10:27:28Z")

</div>

> [@KarolisL](#):
>
> But my inject doesn't establish connection:  
> ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/947215fc3e07142c0b47e59d4a7e08329043efb6.png)
> 
> Is there anything else I'm missing?

1. where are you sending this message (to an -in or -out)? (hint try both)
2. it is recommended you disable "connect automatically" to avoid the attempted connection with incomplete settings. use the connect action to switch it on.
3. `topic` is not required (does nothing on the connect action)

---

<div class="post-metadata">

### Author: ![KarolisL](https://avatars.discourse-cdn.com/v4/letter/k/a4c791/32.png) [@KarolisL](https://discourse.nodered.org/u/KarolisL)
#### Post date: [29 February 2024 10:36 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/6 "2024-02-29T10:36:59Z")

</div>

It worked after I disabled "Connect automatically"

---

<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: [14 March 2024 10:37 UTC](https://discourse.nodered.org/t/mqtt-publish-inject-credentials/86017/7 "2024-03-14T10:37:55Z")

</div>

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