# Help with send MQTT with a short lived JWT

**URL:** <https://discourse.nodered.org/t/help-with-send-mqtt-with-a-short-lived-jwt/6211>\
**Category:** General\
**Created:** [28 December 2018 22:46 UTC](https://discourse.nodered.org/t/help-with-send-mqtt-with-a-short-lived-jwt/6211 "2018-12-28T22:46:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![riplatt](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@riplatt](https://discourse.nodered.org/u/riplatt)\
**Post date:** [28 December 2018 22:46 UTC](https://discourse.nodered.org/t/help-with-send-mqtt-with-a-short-lived-jwt/6211/1 "2018-12-28T22:46:47Z")

</div>

Hi,  
I'm try to write a flow that send MQTT data using a short lived JWT?

The problem I running into is that the standard MQTT connection node has a static user and password and I need to change it every-time the JWT expires.

So the path I have set down is to write the broker connection in a function node, I've add the `require("mqtt")` to the `setting.js`

```javascript
functionGlobalContext: {
    // os:require('os'),
    // jfive:require("johnny-five"),
    // j5board:require("johnny-five").Board({repl:false})
    mqtt:require("mqtt")
},

```

and a function node with the connection setup, also adding the client object to flow context so it can be used by the publish and subscribe function, like:

```javascript
node.status({fill:"red",shape:"ring",text:"disconnected"});
var mqtt = global.get('mqtt');

var options = {
    host: 'mqtt.example.com',
    port: 8883,
    protocol: 'mqtts',
    keepalive: 10000,
    clientId: 'Client001',
    username: (flow.get("access_token") || 0),
    password:'NotUsed'
}

var client = mqtt.connect(options);
flow.set("mqtt_client", client);

client.on('connect', function () {
   node.status({fill:"green",shape:"dot",text:"connected"}); 
   msg.client = client;
   return msg;
})

client.on('reconnect', function () {
   node.status({fill:"yellow",shape:"ring",text:"connecting"}); 
   msg.client = client;
   return msg;
})

client.on('close', function () {
   node.status({fill:"red",shape:"ring",text:"disconnected"}); 
   client.end();
   msg.client = client;
   return msg;
})

return msg;

```

This seems to work the status updates on change of state but no `msg` are returned.

same for the subscribe function:

```javascript
client = flow.get("mqtt_client") || 0;

if (client.connected) {
    node.status({fill:"green",shape:"ring",text:"connected"});
    client.subscribe('test/function/client', function (err) {
        if (err) {
            node.status({fill:"yellow",shape:"ring",text:"error"});
        } else {
            node.status({fill:"green",shape:"dot",text:"subscribed"});
        }
    });
} else {
    node.status({fill:"red",shape:"ring",text:"disconnected"});
    flow.set("mqtt_client", client);
}

client.on('message', function (topic, message) {
    node.status({fill:"green",shape:"dot",text:"message: " + message.toString()});
    msg.topic = topic.toString();
    msg.playload = message.toString();
    return msg;
})

```

and for completeness the publish function

```javascript
client = flow.get("mqtt_client") || 0;

if (client.connected) {
    node.status({fill:"green",shape:"ring",text:"connected"});
    client.publish('test/function/client', 'Hello mqtt', function (err) {
        if (err) {
            node.status({fill:"yellow",shape:"ring",text:"error"});
        } else {
            node.status({fill:"green",shape:"dot",text:"published"});
        }
    });
} else {
    node.status({fill:"red",shape:"ring",text:"disconnected"});
    flow.set("mqtt_client", client);
}

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [28 December 2018 23:11 UTC](https://discourse.nodered.org/t/help-with-send-mqtt-with-a-short-lived-jwt/6211/2 "2018-12-28T23:11:21Z")

</div>

You are returning messages from inside callback functions. That is not the same as returning a message from the function node itself.

The docs tell you how to send messages asynchronously. [https://nodered.org/docs/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/writing-functions#sending-messages-asynchronously)

---

<div class="post-metadata">

**Author:** ![riplatt](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@riplatt](https://discourse.nodered.org/u/riplatt)\
**Post date:** [29 December 2018 02:02 UTC](https://discourse.nodered.org/t/help-with-send-mqtt-with-a-short-lived-jwt/6211/3 "2018-12-29T02:02:43Z")

</div>

Thanks,  
Don't know how I missed that one, thanks again.

Regards
