How can i get browser ip by dashboard button

Hello :smiling_face_with_three_hearts:
I want to get user's ip address when they click a ui dashboard button
Is it possible?

something like this

[{"id":"97969eea.cb323","type":"ui_button","z":"bf9e1e33.030598","name":"","group":"2d4fe667.28f8ba","order":23,"width":0,"height":0,"passthru":false,"label":"button","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":210,"y":680,"wires":[["1e63a505.ef10bb"]]},{"id":"1e63a505.ef10bb","type":"http request","z":"bf9e1e33.030598","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://api.ipify.org/?format=json","tls":"","persist":false,"proxy":"","authType":"","x":370,"y":680,"wires":[["18cc965d.0a748a"]]},{"id":"18cc965d.0a748a","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":680,"wires":[]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

[edit] former script returned the server ip. For browser ip just use a http request to ipify.org

@E1cid ipify return the IP of your router not your lan IP address

Thank @E1cid @zenofmud
This my solution

I register a "button_ip" event in node-red-dashboard/ui.js

And then register "send_ip" event in node-red-dashboard/nodes/ui_button.js
图片

Finally, emit button_ip event in beforeSend in node-red-dashboard/nodes/ui_button.js

[{"id":"da986ef69ef56bfe","type":"ip","z":"c8e31866.cd8f48","name":"ip","https":false,"timeout":"5000","internalIPv4":true,"internalIPv6":false,"publicIPv4":false,"publicIPv6":false,"x":390,"y":3860,"wires":[["0483e027507683ed"]]},{"id":"c3c2f28a16769d2b","type":"inject","z":"c8e31866.cd8f48","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payloadType":"date","x":190,"y":3860,"wires":[["da986ef69ef56bfe"]]},{"id":"ef3c6d37a3ac180d","type":"ui_text","z":"c8e31866.cd8f48","group":"7f99f19f.0f953","order":11,"width":0,"height":0,"name":"","label":"IP-Adress","format":"{{msg.payload}}","layout":"row-spread","className":"","x":1100,"y":3860,"wires":[]},{"id":"0483e027507683ed","type":"change","z":"c8e31866.cd8f48","name":"","rules":[{"t":"move","p":"msg.payload.internalIPv4","pt":"msg","to":"msg.payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":3860,"wires":[["b10c05ea47ddb1cc"]]},{"id":"b10c05ea47ddb1cc","type":"display property","z":"c8e31866.cd8f48","name":"","property":"msg.payload","showDate":0,"showTime":0,"x":920,"y":3860,"wires":[["ef3c6d37a3ac180d"]]},{"id":"7f99f19f.0f953","type":"ui_group","name":"System","tab":"8b657473.5de3e8","order":1,"disp":false,"width":"6","collapse":false},{"id":"8b657473.5de3e8","type":"ui_tab","name":"System","icon":"dashboard","order":2}]

node-red-contrib-ip
node-red-contrib-display-property

Might be interesting :slight_smile:

@WBr that gives you the IP of the device running NR not the IP of the machine with the dashboard open.

So if the browser is running on machine A and Node-RED is running on machine B you get the IP for machine B.

1 Like

Ah sorry misunderstood the question then :slight_smile:

Can't test at right now, but doesn't ui control give the ip address of browser ?

@smcgann99 Great idea! @wooAo, It does return the IP of the browser device!
Here is from my desktop:

{"payload":"connect",
"socketid":"OOp2XSxKv-wiGbioAAAP",
"socketip":"192.168.48.90",
"_msgid":"770f457a8b1bd605"}

and here is from my phone:

{"payload":"connect",
"socketid":"N8X5-wwSXn0qvkEaAAAR",
"socketip":"192.168.48.234",
"_msgid":"f1fd7e8a1bf0d8a2"}

@zenofmud yes,but then all the button will return a msg when you click one button :rofl: :rofl:
maybe i should emit "button_ip" event in button click listener, not in beforeSend, but i haven't find how to do that

@smcgann99 ui-control just gives me msg when client enter the dashboard, but it cannot show me which client is clicking the button, which is important because there is a need that record every user's operation.

You might store the ip in a context variable like

{„socketID“:“socketIP}

When you receive the button click you get the socketid and can lookup the ip adress.

@Christian-Me you are right. but it is hard to operate, because you don't know how many clients there will be and a nother quesiton, socketids to ip is ok, but will one socketid correspond one more ip?

Yes ... you are right ... there could be many clients (browser(s) tabs) connected via one ip-address. But nevertheless if you like to distinguish different devices this might work
image
First you build your ipLookupTabel

var ipLookupTable = flow.get('ipLookupTable') || {};
switch (msg.payload) {
    case 'connect' :
        ipLookupTable[msg.socketid]=msg.socketip;
        break;
    case 'lost' :
        delete ipLookupTable[msg.socketid];
        break;
}
flow.set('ipLookupTable', ipLookupTable);
return msg;

which should build a table with all currently connected clients
image

To get the ip you only have to use this table

var ipLookupTable = flow.get('ipLookupTable') || {};
msg.socketip = ipLookupTable[msg.socketid];
return msg;
[{"id":"f61562a9eea5ee0c","type":"ui_ui_control","z":"d2952ff54c89c139","name":"","events":"all","x":660,"y":1740,"wires":[["d70760f61cd06a0d"]]},{"id":"d70760f61cd06a0d","type":"function","z":"d2952ff54c89c139","name":"ipLookupTable","func":"var ipLookupTable = flow.get('ipLookupTable') || {};\nswitch (msg.payload) {\n    case 'connect' :\n        ipLookupTable[msg.socketid]=msg.socketip;\n        break;\n    case 'lost' :\n        delete ipLookupTable[msg.socketid];\n        break;\n}\nflow.set('ipLookupTable', ipLookupTable);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":860,"y":1740,"wires":[[]]},{"id":"f83112a998c77325","type":"ui_button","z":"d2952ff54c89c139","name":"Test","group":"f5e2c3bf3d5a99b2","order":6,"width":"0","height":"0","passthru":false,"label":"Test","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"clicked","payloadType":"str","topic":"a topic","topicType":"str","x":670,"y":1780,"wires":[["220dd878d1e6aed4"]]},{"id":"220dd878d1e6aed4","type":"function","z":"d2952ff54c89c139","name":"get Ip","func":"var ipLookupTable = flow.get('ipLookupTable') || {};\nmsg.socketip = ipLookupTable[msg.socketid];\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":830,"y":1780,"wires":[["2924257c32df2382"]]},{"id":"2924257c32df2382","type":"debug","z":"d2952ff54c89c139","name":"Test","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"socketip","statusType":"msg","x":1010,"y":1780,"wires":[]},{"id":"f5e2c3bf3d5a99b2","type":"ui_group","name":"Default","tab":"450ac4c9b5c2acc6","order":1,"disp":true,"width":"11","collapse":false},{"id":"450ac4c9b5c2acc6","type":"ui_tab","name":"test","icon":"dashboard","disabled":false,"hidden":false}]

To be honest I haven't read the complete thread but it should solve the problem with basic node-red & dashboard nodes. But perhaps this is not what you are looking for :wink:

Little note: This approach don't lead to a multi user dashboard (I learned it the hard way) same as other approaches. All inputs give you a socketid from where the input originates but ALL outputs go to ALL clients whatever socketid you send. This is how it is and it is by purpose - The dashboard is the representation of the state of node-red. But something like a "read only" dashboard for certain devices is possible.

No, every socketid correspond only to ONE ip address. As the name suggest an ID is unique. If you have fixed ip addresses (or can do reverse DNS) you can determine single devices securely.
The socketid changes on every connect (even browser refresh) but the socketip stays constant (until the network interface gets a new address by DHCP)

@Christian-Me :wink: :wink:okey. thank you, i'll try your solution.

1 Like

I use Ui-control with connect messages to store the ip and socket id of clients.
if you look at debug from a button click you get -

object
payload: 1645370401433
event: object
clientX: 882
clientY: 864
bbox: array[4]
socketid: "2ldTknYmnRL2Cv_IAAOd"
_msgid: "47971bd610c4dabb"

You can use this to lookup the ip ?

Sorry @Christian-Me I didn't notice you had already expanded on this :wink:

And? Are the results of your tests positive? Only curious :wink:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.