Not able to connect eu.thethings.network from Javascript Paho Socket

Hello,

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();
});

Hi @pml, I see that your topic is:

var topic = "/devices/+/up";

I think that it should be:

var topic = "+/devices/+/up";

Assumes that you want to subscribe to a feed of all uplink messages from all devices registered on the TTN application.

Hello @cultsdotelecomatgmai , I dont think that will be problem because I am not able to connect after connecting the topic part will come.

Moreover this is the mosquitto CLI then I am able to connect

mosquitto_sub -d -h eu.thethings.network -t +/devices/+/up -u 'sr-ops-rtr-01' -P 'ttn-account-v2.pgyU1<redacted>tkU'

But not be able to connect from Poho socket

Is that the TCP/IP port? Why not using the standard 1880? Surely the TTN server is not listening on port 10.

I have tried with the port 1880, still its not working ans showing the same error as AMQJSC0001E Connect timed out

My bad, it’s 1883. But you’re also using TLS, then it’s probably 8883.

Sir, Its still not working. I have already tried with these port.

Well, then please start with explaining what else you already tried?

https://www.thethingsnetwork.org/docs/applications/mqtt/api.html

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.

If this is about websockets: those are not supported by TTN.

Sir, then what will be the alternative to get the same data in the http. Can you please help me with this.

It still wasn’t clear to me if this library was using WebSockets, but given the following from their website I guess it is:

image

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:

Maybe the following helps: https://github.com/zoutepopcorn/ttn_tools/blob/master/README.md (But that still references the obsolete staging.thethingsnetwork.org, which you should replace with details from https://www.thethingsnetwork.org/docs/applications/mqtt/api.html instead.)

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.)