Reader small image

You're reading from  Practical Python Programming for IoT

Product typeBook
Published inNov 2020
Reading LevelIntermediate
PublisherPackt
ISBN-139781838982461
Edition1st Edition
Languages
Right arrow

Connecting to the broker

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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Practical Python Programming for IoT
Published in: Nov 2020Publisher: PacktISBN-13: 9781838982461