I am able to connect through the mosquitto_sub but when I am using Paho Eclipse to connect the MQTT broker I am not able to connect I am getting an error AMQJSC0001E Connect timed out. I dont know Whats wrong with my code because its seems everything perfect. Here is the script what I used. Can any body help me how to solve this issues. Surely I am missing something.
var host = "eu.thethings.network";
var username = XXXXXXX";
var password = "ttn-account-v2.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var topic = "/devices/+/up";
cleansession = "";
var port = 10;
var mqtt;
var reconnectTimeout = 2000;
function MQTTconnect() {
console.log("Connecting to "+ host + " " + port)
if (typeof path == "undefined") {
path = '/devices/up';
}
mqtt = new Paho.MQTT.Client(
host,
Number(port),
path,
"piyush_" + parseInt(Math.random() * 100, 10)
);
var options = {
timeout: 3,
useSSL: true,
cleanSession: false,
onSuccess: onConnect,
onFailure: function (message) {
console.log(message);
$('#status').val("Connection failed: " + message.errorMessage + "Retrying");
setTimeout(MQTTconnect, reconnectTimeout);
}
};
mqtt.onConnectionLost = onConnectionLost;
mqtt.onMessageArrived = onMessageArrived;
if (username != null) {
options.userName = username;
options.password = password;
}
console.log("Host="+ host + ", port=" + port + ", path=" + path + " username=" + username + " password=" + password);
console.log(options);
mqtt.connect(options);
}
function onConnect() {
alert("connected");
$('#status').val('Connected to ' + host + ':' + port + path);
// Connection succeeded; subscribe to our topic
mqtt.subscribe(topic, {qos: 0});
$('#topic').val(topic);
}
function onConnectionLost(response) {
setTimeout(MQTTconnect, reconnectTimeout);
$('#status').val("connection lost: " + responseObject.errorMessage + ". Reconnecting");
};
function onMessageArrived(message) {
var topic = message.destinationName;
var payload = message.payloadString;
$('#ws').prepend('<li>' + topic + ' = ' + payload + '</li>');
};
$(document).ready(function() {
MQTTconnect();
});
I am using Paho Socket , as per my client its listening in Port 10 , and he has given me the mosquitto_sub command to connect, But I need to get the data in the web so that i can make the rest API and use in the app.
Using mosquitto sub I am able to connect and I am getting the data which i want but I need same thing in web.
I have written the sample script above in the question which I am using.
It still wasn’t clear to me if this library was using WebSockets, but given the following from their website I guess it is:
Also, I understand that you want to access the data from a browser? (JavaScript is not only used in web browsers. In a browser, you indeed cannot use MQTT as browsers don’t have native support for that, and one cannot use other protocols such as HTTP to mimic MQTT client-side.)
You cannot get TTN data into a browser directly when using MQTT:
If not, then you’ll need to get the data to some other (your own) server first, and then make your website query that data. If you cannot use MQTT on the server side, then see the available integrations. But none will allow direct access from a web page, if only as that would require you to make the secret credentials public to anyone who’s smart enough to peek into your client-side JavaScript code. (So, even the Storage integration should not be used from a public web page directly. But as that only stores 7 days of data anyway, it’s probably not sufficient anyhow. You’ll really need some server.)