MQTT in Javascript.

Himalaya Pagada
May 20, 2022

Follow the below code to implement MQTT using Javascript.

<script src=”https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.min.js" type=”text/javascript”></script><script type=”text/javascript” language=”javascript”>client = new Paho.MQTT.Client(“192.168.103.141”, 9001, “/ws”, “webdqweqe”);client.onConnectionLost = onConnectionLost;client.onMessageArrived = onMessageArrived;var myVar;// connect the clientclient.connect({ onSuccess: onConnect, onFailure: onFailure });function onFailure(e) {console.log(e);}function onConnect() {clearInterval(myVar);console.log(“onConnect”);client.subscribe(“rfidNo”);}// called when the client loses its connectionfunction onConnectionLost(responseObject) {if (responseObject.errorCode !== 0) {console.log(“onConnectionLost:” + responseObject.errorMessage);}myVar = setInterval(function () { sendTimer() }, 5000);}function sendTimer() {client.connect({ onSuccess: onConnect });}// called when a message arrivesfunction onMessageArrived(message) {console.log(“onMessageArrived:” + message.payloadString);}</script>

--

--