TTN Node.js API Quick Start Start guide

Hello, I have been trying to see the data from my devices on the system console using the quick-start guide:-

https://www.thethingsnetwork.org/docs/applications/nodejs/quick-start.html

I have followed the steps and run node.js on an index.js file according to the getting started guide.
The program launches(I have added code to make it display “Program running” on the console), then just sits there. The program doesn’t print anything.

I can see data in the TTN Console being received from two devices, but can’t see this on the system console of the machine that I running the node application on. I have tried 2 different Ubuntu platforms, to no effect.

This is the standard code from the Quick Start example:-

ar ttn = require("ttn")

var appID = "testxxxx"
var accessKey = "ttn-account-v2.Et5XXXXXXXXXXXXXXXXXXXXXXXX"

console.log("Program running")

ttn.data(appID, accessKey).then(function (client) {
        client.on("uplink", function (devID, payload) {
            console.log("Received uplink from ", devID)
            console.log(payload)
        })
    })
    .catch(function (error) {
        console.error("Error", error)
        process.exit(1)
    })

I am not a Javascript expert, but can’t really see where I am going wrong. It appears that I am not making a connection to the TTN Server.
Is there something very fundamental that I have missed?

I have also tried to connect using a Ruby MQTT gem., but with no success.

Any pointers?

Regards,
JimGi

Seems to work for others; see Node.js and TTN. So check and re-check the appID and accessKey. Also: do you have any nodes registered to that application, which are sending data…?

Hello, thanks - I did see that, and have used the same code as suggested by the respondent.
I have set the appID and accessKey, and my nodes are sending data, I can see it in the TTN console.
Will check and re-check these again as suggested.
jimgi.

Your code is working for me too. Did you install the latest version?

On my Mac:

npm list ttn
my-ttn-quickstart@1.0.0
└── ttn@2.3.1 

node --version
v9.5.0

npm --version
5.6.0

Also make sure that the key you’re using has at least the label “messages” next to it:

image

And to use the right application id:

image

Thank you for doing that - I will check it out.
Jimgi

Thats a good summary.
I have:

  1. ttn version 2.3.1
  2. node 4.4.5 - possibly too old?
  3. npm version 2.15.5 - again, too old?

So, it appears that I need to update node and npm.
I tried on another platform (Brixx - NUC style) and updated node/npm to the latest stable releases, but will double check.

I do have messages next to the access keys.

Thanks again.
jimgi

That’s May 2016, but still: the TTN library will have downloaded its own dependencies (which you can find in the node_modules folder of your project), and you did not get any errors there…

NPM is just the package manager; no problem if that’s a bit outdated.

Also, a non-existing appID will get you Error: 5 NOT_FOUND: discovery:app_id:bogus-id not found and the wrong key Error: Connection refused: Not authorized, so I’m out of ideas…

That sounds logical - I must be doing something fundamentally wrong, or there is an error somewhere in my network or implementation that I haven’t detected…

I will also try on my other platform, x86 based with application version numbers similar to yours.
I have checked and rechecked myappID and accessKeys, and entered them using cut and paste from the console, so pretty sure that they are correct.

Turned off firewall - no change.

jimgi

It’s using MQTT, so you could also try an MQTT client first.

It seems to be getting the MQTT endpoint URLs from some discovery.thethingsnetwork.org:1900, and is using a secure connection for that. I wonder if the certificates on your computer might be outdated, or some ports might be blocked? But then I’d expect an error.

You could add more events to report on; see https://www.thethingsnetwork.org/docs/applications/nodejs/api.html#dataclient (which also mentions mqttAddress, which is somehow determined automatically).

Still stuck with no data. Tried putting the device in a DMZ and also enabling various ports for UDP, but no success. Not sure what ports to allow through my router/firewall. 1700, 1883?
I have 2 separate systems, and they both behave exactly the same.
Also tried a Ruby TTN client, and again, no joy.
Couldn’t see any activity using wireshark, but then not a competent user.
Something is blocking it somewhere…

jimgi

I doubt the discovery is using UDP, but I’ve not looked at what it does. MQTT certainly is not using UDP; it uses TCP/IP to connect to port 1883 (or port 8883 for TLS), and does not need to have any ports open for incoming connections. I’d try an MQTT client first.

Hello again,
Thanks for your input. I found a Ruby MQTT client that I was able to communicate with the Mosquitto test site successfully…still can’t make it connect to TTN though.
Haven’t had much chance to look at it this weekend, but will keep plugging away.
Jimgi

It’s easy to miss the

  • CD NPM

command before installing the Node-red package…

Finally managed to get something working, but no idea why my original code doesn’t work…went back to the API as you suggested.

var ttn = require('ttn');
var appId = 'test';
var accessKey = 'ttn-account-v2.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

var client = new ttn.DataClient(appId, accessKey, 'eu.thethings.network:1883');

//console.log(client)
//client.on('uplink', function (msg) { console.log('Received message', msg);});

client.on("uplink", function (devID, payload) {
    console.log("*** Received uplink from*** ", devID)
    console.log(payload)
    console.log()
})

Simply creating a new DataClient object seemed to solve the problem.
I am running node.js version 8.9.4.

Still can’t get the original code to work.
jimgi

I guess that’s also what the data helper does eventually, but that first uses some discovery to find the URL, and: that code uses async/await, which apparently was only added in Node 7, and was experimental up to Node 7.6, it seems.

So maybe the old Node is to blame after all, but one would certainly expect some error then…

Yes, I agree. On 2 platforms ( ARM and x86) the process appeared to hang, without any further notification at all.
At lease I can move forward now…but may still investigate further to see if I can pin it down. Also keen to try to make things work with Ruby on Rails - but that seems to be less popular in this environment.
Thanks for the input!
Jimgi

Hello arjanvanb
I am use: https://www.thethingsnetwork.org/docs/applications/nodejs/quick-start.html
I am using in win10
When I run the command: “npm install --save ttn”
image
Node version: v10.16.0
npm version: v6.9.0
Would you know what I might be doing wrong?
I checked that the ttnapi dependency is “not found”:
https://www.npmjs.com/package/ttnapi
Thank you and Best Regards!

It seems you don’t have git installed.

Hello
I solved this issue.
I needed add GIT in PATH in environment variable of win10 before install ttn.
Thank you