Node flow to convert ip to coordinates?

I cannot find a good example of IP address to coordinates using a geoip service, is this possible?
Cheers,
Jacq

You get the IP address of a visitor of your dashboard via the node ui_control in msg.socketip.
Then use a service like https://ipstack.com/product.
I just have registered there for the free plan and playing with their API service, and it looks promising!
Node-RED will make the http request for you and ipstack will return the geo location.

I just tried the Google name server and got this json return:

{"ip":"8.8.8.8","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":"CA","region_name":"California","city":"Mountain View","zip":"94041","latitude":37.38801956176758,"longitude":-122.07431030273438,"location":{"geoname_id":5375480,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}

When using the request with Firefox, Firefox seems to convert my http request to a https request and I get the following error from ipstack:

Access Restricted - Your current Subscription Plan does not support HTTPS Encryption.

You could install a suitable NPM module, such as https://www.npmjs.com/package/geoip-lite to your Node-RED home directory (assuming Linux host):

cd .node-red
npm install geoip-lite

Then require it inside your settings.js and then use it in a function node (more info: https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules) :

const geoip = global.get('geoip-lite');
 
let ip = "207.97.227.239"; // read this from a msg property sent with msg
msg.payload = geoip.lookup(ip);
 
return msg;

Note: I didn't test this, just did a quick web search and modified the code example from NPM.

1 Like

This would make a good contrib node. Maybe there is one already but I didn't find with a quick search.

1 Like

@BartButenaers I saw that! I almost tipped you on the message. :grimacing: The forums official wizard 24 hours turnaround contrib node creator! :smile:

1 Like

Hi @ristomatti,
Indeed the last 3 years I definitely would have started immediately developing this node (like a crazy maniac), so you guys would be able to test it tomorrow ...
However lately I have found a much better tactic: I try to find weak victims which I believe have lots of potential, and I try to convince and assist them to write their own (first?) custom node.
Guys like you :joy:
That is my new way to increase the population of node developers, since I don't have enough energy left to do a live stream about Node-RED development work every monday at 8pm BST :rofl:
If you are interested, send me private message :shushing_face:
Bart

5 Likes

Joking aside, I think you've done more than enough for the community. :slightly_smiling_face: Just take it easy and create stuff that tickles your current interest!

If you check node-red-contrib-node-lifx you'll notice I'm the maintainer (not original developer) and also the maintainer and co-developer of the module it wraps so it wouldn't be a problem for me to develop this custom node. However what drives me coding on my free time is a personal need and I unfortunately don't have a need for this. Therefore a quick hack with a function node shall suffice. :smile:

Thanks for the offer anyway!

1 Like

Haha, this discussion - for some undefinable reason - tickled my interest :wink:

Since you already did the analysis, writing the node itself was only half an hour work...
However I would like to have a nice way to download the data files automatically and detect when they have finished downloading. I have raised an issue in the geoip-lite repository, so now I have to wait until I get an answer.

Meanwhile people can use your function node solution...

1 Like

:rofl: ... Have to say difficult for me to understand how you can put stuff out so fast! Especially the long documentation. I hate writing documentation. :roll_eyes:

After a while it is a lot of copy-paste, since you have most of it already programmed already for other nodes... Documentation is a necessity: you have to digg into such a detailed level, that I can never remember after some time how it worked. Have to read every time again through my own readme files :shushing_face:

2 Likes

I figured that was the reason and it's smart! But I still can't get myself to write any more than the bare minimum.. :joy: Instead I'll try to do small commits and capture some of the decisions within the comments so I can get inline info of what feature/issue a particular change solves (through git blame). :wink:

1 Like

You can install a beta version of the node-red-contrib-ip-location-lite node directly from my Github account:

npm install bartbutenaers/node-red-contrib-ip-location-lite

Note that I still cannot determine automatically whether the download of the new data files from Maxmind is completed. Hopefully somebody responds to my geoip-lite issue to solve this ...

Have fun with it!

I gave it a go and it worked on first try, well done! It was a bit of a surprise to have to create an account as I only quickly skimmed through the geoip-lite module documentation and there wasn't any license key used in the example. Now I see that updating the database is optional - did you try if it already comes with a relatively up-to-date database?

Also after registering and logging in on GeoIP's site, they had a prompt to add billing information "for continued use of our services" - so I wonder if it's free only a limited amount of time...

Yes but I got a bit of different locations then... Perhaps a bit older version of the data files? Not sure ...

Ah, can't remember that I have seen that...

OK so then it sounds like the database is too old.

I have found a solution myself...
My repository is updated on Github. With this new version, you can inject an ``msg.payload="update"` to update the local database. Not that it can take about half an hour to update the 100 Mb database on a Raspberry! Meanwhile the status will be "updating..." (with animated dots to show the progress), and at the end the status will be "updated".

image

Would be nice if somebody could test this, so I can publish it on NPM afterwards.

Hi Bart, did a quick test. Observations:

  • Update msg is not documented in the sidebar documentation
  • When I sent the upload payload, the node status started to indicate it's updating but also sent a message "The node is already busy downloading the data" although the "Updating..." status was not visible

Hi Aero,
Thanks for your help!

It is now added:

image

In my tests it works always like this:

  1. In inject an "update" message, to start the DB update.
  2. Then the node status becomes "updating ...".
  3. When I sene a new "update" message - while the previous update is still busy - you will get the warning "The node is already busy downloading the data"

Not sure why it doesn't work in your case. I have added some extra child process cleanup code on Github, but not sure whether it will solve it. If you would be able to test it again, please share in detail which steps you have executed and when it goes wrong ...

I just realised what happened. I'm running 1.1.0-beta.1 and tested this on my tablet. The current beta has a bug that causes inject node taps to register twice. So it shouldn't be an issue with your node. Just push to npm!

1 Like