# Closing mqtt instance in a function node

**URL:** https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893
**Category:** General
**Tags:** docker, mqtt
**Created:** [24 January 2024 22:35 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893 "2024-01-24T22:35:25Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [24 January 2024 22:35 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/1 "2024-01-24T22:35:25Z")

</div>

I am trying to use a function node and the [mqtt](https://www.npmjs.com/package/mqtt) package in a function node.  
So my code is executed on start (which is probably what's causing [problems](https://discourse.nodered.org/t/node-red-not-responsive-hangs-on-starting-flows/84699) starting the flows here).  
I do not have any code that handles closing mqtt connection/, the instance is started upon a deployment and never exited.  
And what's even funnier , the code is doing console.log on connection. after first redeploy (restarting on docker, then redeploying), and after each redeploy i am seeing another connection in my EMQX dashboard.  
And the message is received successfully and processed to the output (only one, a little bit slower than when we have only one client connected)

How can i successfully close a mqtt connection from a function node.  
Tried saving the mqtt.connect object in context, and using it on stop, but no luck.

Here is the code:

```auto

    // If no existing client, create a new one
    var client = mqtt.connect(`${env.get('configuration').protocol}${env.get('configuration').url}`, {
        port: env.get('configuration').MQTTport,
        username: env.get('configuration').MQTTusername,
        password: env.get('configuration').MQTTpassword,
        clean: true
    });

    client.on("connect", function () {
        console.log("connected")
        node.status({fill:"green",shape:"dot",text:"connected"});
    
    });

client.on("error", function (error) {
    node.status({fill:"red",shape:"ring",text:"disconnected"});
    console.log(`modbus-calendar-MQTT: ${env.get('configuration').hvacId} \n ${error}`);
    if (error.toString().includes('Authentication failed')) {
        var delay = Math.random() * (60000 - 20000) + 20000; 
        setTimeout(() => {
            console.log("Attempting to reconnect...");
            client.reconnect();
        }, delay);
    }
});
client.subscribe(`modbus-calendar/${env.get('configuration').hvacId}/#`, { qos: 2 });
    client.on('message', function (topic, message, packet) {
        msg.received = JSON.parse(message).Data;
        msg.topic = topic;
        node.send(msg);
    });

```

EDIT #1 I know i can turn on the **Use Username as Client ID** and only keep one connection for a username, but designing to run the flow on a raspberry pi, and actually have not tested if this haves a particular memory impact.

EDIT #2 When **Use Username as Client ID** is enabled after each deploy / restart flows memory memory usage is increased by 3.9-4.2mb (not happening when all flows disabled). The increase is 2.8-3.2mb when **Use Username as Client ID** is disabled.

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [24 January 2024 23:17 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/2 "2024-01-24T23:17:26Z")

</div>

> [@lizzardguki](#):
>
> I am trying to use a function node and the [mqtt](https://www.npmjs.com/package/mqtt) package in a function node.

Wouldn't it be simpler to use the MQTT nodes?

---

<div class="post-metadata">

### Author: ![lizzardguki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lizzardguki/32/103637_2.png) [@lizzardguki](https://discourse.nodered.org/u/lizzardguki)
#### Post date: [24 January 2024 23:19 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/3 "2024-01-24T23:19:47Z")

</div>

Yes, but then i do not have an option to dynamically pass any options in dynamically 😕

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [24 January 2024 23:24 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/4 "2024-01-24T23:24:24Z")

</div>

#### Dynamic Subscription

The node can be configured to dynamically control the MQTT connection and its subscriptions. When enabled, the node will have an input and can be controlled by passing it messages.

### [Inputs](http://192.168.1.28:1880/#)

These only apply when the node has been configured for dynamic subscriptions.

action string  
the name of the action the node should perform. Available actions are: `"connect"`, `"disconnect"`, `"subscribe"` and `"unsubscribe"`.  
topic string|object|array  
For the `"subscribe"` and `"unsubscribe"` actions, this property provides the topic. It can be set as either:

- a String containing the topic filter
- an Object containing `topic` and `qos` properties
- an array of either strings or objects to handle multiple topics in one

broker broker  
For the `"connect"` action, this property can override any of the individual broker configuration settings, including:

- `broker`
- `port`
- `url` - overrides broker/port to provide a complete connection url
- `username`
- `password`

If this property is set and the broker is already connected an error will be logged unless it has the `force` property set - in which case it will disconnect from the broker, apply the new settings and reconnect.

---

<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: [25 January 2024 00:21 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/5 "2024-01-25T00:21:40Z")

</div>

![mqtt-dyn](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9f75aefbc86e548c56848ae497773e878cccacd.gif)

See here: [Dynamic MQTT node's "actions-\>connect" guidance needed - #2 by Steve-Mcl](https://discourse.nodered.org/t/dynamic-mqtt-nodes-actions-connect-guidance-needed/58878/2)

---

<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: [8 February 2024 00:22 UTC](https://discourse.nodered.org/t/closing-mqtt-instance-in-a-function-node/84893/6 "2024-02-08T00:22:09Z")

</div>

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