Search code examples
javascriptmqttpahotizen-web-app

Paho MQTT JS Client already connected error?


I'm trying to build a Tizen Web Application which connects to an MQTT broker using Paho MQTT JS library.

But every time I run the application I keep getting the following error:

Error: AMQJS0011E Invalid state already connected.

Here is my code

/**
 * 
 */

var broker="broker.mqttdashboard.com";
var port=8000;

var client=new Paho.MQTT.Client(broker,port,"myWebClientID_"+parseInt(Math.random()*100,10));

client.onConnectionLost=function (response){
	console.log("Connection Lost: ",response.errorMessage);
}

client.onMessageArrived=function(message){
	console.log(message.destinationName, "--",message.payloadString);
}

var option={
		timeout:3,
		onSuccess: function(){
			client.subscribe('/Aya',{qos:1});
			console.log("Connected to broker");
			
			var message=new Paho.MQTT.Message("Test Message");
			message.destinationName="/Aya";
			client.send(message);
			
		},
		onFailure:function(msg){
			console.log("Connection Failed",msg.errorMessage);
		}
}

function init(){
	client.connect(option);
}

$(document).ready(function(){
	console.log("Document ready");
	init();

});

The code snippet works fine when I'm trying to run it on a browser.


Solution

  • I realized that the error had nothing to do with the MQTT error.

    The issue was that I hadn't given the application appropriate privileges, i.e. I did not add the application permission to access the Internet in config.xml and therefore the application could not connect to the broker.