javascriptmqttmqtt.js

MQTT describe function outside the handler


I am working on MQTT https://www.emqx.com/en/blog/mqtt-js-tutorial#mqtt-js-api-introduction

This is how i connect.

const client = mqtt.connect(host, optionsMQTT);

client.on('connect', function (connack) {
    console.log('Connected')
    console.log(connack)
});

Is it possible instead of passing this full function as parameter i only pass the name of the function :--

function (connack) {
    console.log('Connected')
    console.log(connack)
}

How to pass only the name of the function instead of full function and make it work ?


Solution

  • The following should work:

    const client = mqtt.connect(host, optionsMQTT);
    
    function connactFunc (connack) {
        console.log('Connected')
        console.log(connack)
    }
    
    client.on('connect', connactFunc);