# Is it possible to configure a single Inject node to connect and subscribe to an MQTT broker using Dynamic Connections?

**URL:** <https://discourse.nodered.org/t/is-it-possible-to-configure-a-single-inject-node-to-connect-and-subscribe-to-an-mqtt-broker-using-dynamic-connections/61560>\
**Category:** General\
**Tags:** mqtt\
**Created:** [20 April 2022 20:07 UTC](https://discourse.nodered.org/t/is-it-possible-to-configure-a-single-inject-node-to-connect-and-subscribe-to-an-mqtt-broker-using-dynamic-connections/61560 "2022-04-20T20:07:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shan](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shan](https://discourse.nodered.org/u/Shan)\
**Post date:** [20 April 2022 20:07 UTC](https://discourse.nodered.org/t/is-it-possible-to-configure-a-single-inject-node-to-connect-and-subscribe-to-an-mqtt-broker-using-dynamic-connections/61560/1 "2022-04-20T20:07:15Z")

</div>

I am getting to grips with the concept of Dynamic MQTT connections and related Pub/Sub. However I am curious as to whether it would be possible to use the same **Inject Node** to do the following:

1. Force connect to an MQTT broker
2. Subscribe to a desired topic

I tried doing the following within an inject node:

- `msg.action` set to `connect`
- `msg.broker` set to `{"broker": "internal-mqtt-broker", "port":1883, "force":true}`
- `msg.action` set to `subscribe`
- `msg.topic` set to `$SYS/#`

Upon deploying the flow, the inject node is not able to connect to the Broker.  
Logs:

```auto
node-red | 20 Apr 19:55:55 - [info] Server now running at http://127.0.0.1:1880/
node-red | 20 Apr 19:55:55 - [info] Starting flows
node-red | 20 Apr 19:55:55 - [info] Started flows
node-red | 20 Apr 20:00:47 - [info] Stopping flows
node-red | 20 Apr 20:00:47 - [info] Stopped flows
node-red | 20 Apr 20:00:47 - [info] Starting flows
node-red | 20 Apr 20:00:47 - [info] Started flows

```

Flow Diagram:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a9b8711ebdd49caf0637923c605c22fa06516c5c.png)

Inject Node Properties

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/7/2704a1e017a8dcb7141036ceff7039a07538231b.png)

Whereas upon removing the subscribe action and topic from the Inject node, I am able to connect to the broker and subscribe to a topic via a separate node.

Would there be some way to merge the **connection** + **subscription** actions of the node into one 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:** [20 April 2022 21:59 UTC](https://discourse.nodered.org/t/is-it-possible-to-configure-a-single-inject-node-to-connect-and-subscribe-to-an-mqtt-broker-using-dynamic-connections/61560/2 "2022-04-20T21:59:54Z")

</div>

> [@Shan](#):
>
> Would there be some way to merge the **connection** + **subscription** actions of the node into one node?

Kind of - use a delay to cause 2 messages, the 1st one to connect, the 2nd to subscribe

![chrome_d0H8etCnwR](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21f5d914de134bbf95ddace05b64ec4f90c2e439.gif)

* * *

Why your example doesnt work...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/45b053eb1d4ed5b97bc093ef024084a7dd11d0d3.png)  
... This is easily visible by adding a debug node AFTER the inject (debug node set to "show complete message")

---

<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:** [4 May 2022 22:00 UTC](https://discourse.nodered.org/t/is-it-possible-to-configure-a-single-inject-node-to-connect-and-subscribe-to-an-mqtt-broker-using-dynamic-connections/61560/3 "2022-05-04T22:00:52Z")

</div>

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