We define our onConnectionSuccess() handler at line (5), which will be called after our client successfully connects to the broker. When we successfully connect, we then update the web page to reflect the successful connection and enable the slider control:
onConnectionSuccess = function(data) { // (5)
console.log("Connected to MQTT Broker");
$("#connected").html("Yes");
$("input[type=range].brightnessLevel")
.attr("disabled", null);
client.subscribe(TOPIC); // (6)
};
client.connect({ // (7)
onSuccess: onConnectionSuccess,
reconnect: true
});
Next, at line (6), we subscribe to the led topic. It's at line (7) that we connect to the broker. Notice that we're registering the onConnectionSuccess function as the onSuccess option.
Remember, similar to the Python example, always subscribe to...