UDP msg.ip, msg.port

851 views
Skip to first unread message

Ram

unread,
Sep 26, 2017, 1:47:44 AM9/26/17
to Node-RED
How to use msg.ip and msg.port in my flow?

Colin Law

unread,
Sep 26, 2017, 2:37:08 AM9/26/17
to node...@googlegroups.com
It depends on what you want to do with them. You can access them
directly in a function node, you can, for example, use a change node
to move msg.ip to msg.payload, lots of other ways too. Give us a bit
more information on what you want to do and likely someone will
suggest ways of achieving it.

Colin

On 26 September 2017 at 06:47, Ram <srir...@gmail.com> wrote:
> How to use msg.ip and msg.port in my flow?
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/aae4e894-b16a-4d1a-8ed8-0b5a566c96c7%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ram

unread,
Sep 26, 2017, 2:44:47 AM9/26/17
to Node-RED
I've a sensor that is sending UDP messages to my gateway that's running node-red. I've setup a simple flow with a UDP listener listening on a specific port to which my sensor sends messages. The sensor is using a 3G Sim card. I'm trying to capture the IP and PORT from which the messages are coming from and send an acknowledgement back to the sensor. I'm not sure how to use the msg.ip and msg.port on a function node along with a UDP Out node. Please help. I'm a newbie. 

Colin Law

unread,
Sep 26, 2017, 2:53:22 AM9/26/17
to node...@googlegroups.com
To use msg.ip in a function node just use msg.ip in the same way you
would use msg.payload.

Colin
> https://groups.google.com/d/msgid/node-red/7e345076-fbb7-4b52-a660-1d56978799d9%40googlegroups.com.

Ram

unread,
Sep 26, 2017, 3:03:19 AM9/26/17
to Node-RED
Could you please show me how to do it? I'm not familiar. 

Nick O'Leary

unread,
Sep 26, 2017, 3:54:14 AM9/26/17
to Node-RED Mailing List
Ram,

msg.ip and msg.port are just message properties like anything else. If you are familiar with how you set msg.payload to different values, then the principle is identical.

For example, in a Function node:

msg.ip = "127.0.0.1";
msg.port = 12345;
return msg;



But - you say you are receiving a message from the UDP In node and you want to send a response back to where it came. If you read the help text for the UDP In node it says:

It also provides msg.ip and msg.port set to the ip address and port from which the message was received.

So the message coming out of the UDP node already has msg.ip and msg.port set to the values you need in order to respond.


The UDP Out node's help says:

You may also use msg.ip and msg.port to set the destination values, but the statically configured values have precedence.

Which means, as long as you leave the node un-configured, you use those two message properties to direct the message.


So if you just want to respond to the message, update msg.payload with the data you want to send back and leave the rest of the message alone - it should then just work.

Does that make sense to you?

Nick

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Mark Setrem

unread,
Sep 26, 2017, 3:55:48 AM9/26/17
to Node-RED
There are some great examples of how to write functions in online tutorials

e.g http://noderedguide.com

or there are also a couple of examples on the node-red website.

Take a look at them and if you still need some help, if you post the flow you have we can nudge you in the right direction

Ram

unread,
Sep 26, 2017, 4:00:10 AM9/26/17
to Node-RED
Hi Nick,
Thanks for the detailed explanation. You are right to say that the messages I receive from my sensor node already contains the IP address and PORT number in it. I just need to reply back to it with a simple flow. If I understood your comments below, will such a flow work to reply back?

[{"id":"5b303b71.1600e4","type":"udp in","z":"67e087dc.241378","name":"UDP 5121","iface":"","port":"5121","ipv":"udp4","multicast":"false","group":"","datatype":"utf8","x":228,"y":149,"wires":[["8189896a.022ae8"]]},{"id":"8189896a.022ae8","type":"function","z":"67e087dc.241378","name":"","func":"msg.payload = \"received\";\nreturn msg;","outputs":1,"noerr":0,"x":406.5,"y":135,"wires":[["3be119c8.9e77b6"]]},{"id":"3be119c8.9e77b6","type":"udp out","z":"67e087dc.241378","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":false,"multicast":"false","x":569.5,"y":186,"wires":[]}]
Nick

Dave C-J

unread,
Sep 26, 2017, 4:13:47 AM9/26/17
to node...@googlegroups.com
Hi
yes - something like that should work... but does assume the device is also listening on that same port, which it may well be - but you should check.

Ram

unread,
Sep 26, 2017, 4:28:23 AM9/26/17
to Node-RED
Unfortunately, that didn't work for me :(

Ram

unread,
Sep 26, 2017, 5:19:47 AM9/26/17
to Node-RED
I did exactly the same kind of configuration for my TCP in and TCP out, where the TCP out has a mode called "reply to TCP". It worked flawlessly. However, for UDP, I'm not sure why it won't work. What am i doing wrongly?

Nick O'Leary

unread,
Sep 26, 2017, 5:21:49 AM9/26/17
to Node-RED Mailing List
Hi Ram,

I don't think we can answer that without more specific details of your flow to see what you're actually doing.

Nick

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Ram

unread,
Sep 26, 2017, 5:46:22 AM9/26/17
to Node-RED
Here is my flow:
[{"id":"a0dafd75.1d004","type":"udp in","z":"be6de9f.1769a18","name":"UDP 5121","iface":"","port":"5121","ipv":"udp4","multicast":"false","group":"","datatype":"buffer","x":547,"y":236,"wires":[["b99157.c9dc9ea8"]]},{"id":"bde1ad32.94adb","type":"udp out","z":"be6de9f.1769a18","name":"","addr":"","iface":"","port":"","ipv":"udp4","outport":"","base64":true,"multicast":"false","x":943.5,"y":240,"wires":[]},{"id":"b99157.c9dc9ea8","type":"function","z":"be6de9f.1769a18","name":"","func":"msg.payload = \"received\";\nreturn msg;","outputs":1,"noerr":0,"x":741.5,"y":238,"wires":[["bde1ad32.94adb"]]},{"id":"f414e02d.1b9e4","type":"tcp in","z":"be6de9f.1769a18","name":"","server":"server","host":"","port":"1025","datamode":"stream","datatype":"utf8","newline":"","topic":"","base64":false,"x":421,"y":436,"wires":[["79698e53.310a8"]]},{"id":"dcc26f36.fa1ac","type":"ui_text","z":"be6de9f.1769a18","group":"1ea6e66e.e380ba","order":0,"width":"20","height":"7","name":"","label":"TCP Raw Data","format":"{{msg.payload}}","layout":"col-center","x":932,"y":426,"wires":[]},{"id":"79698e53.310a8","type":"delay","z":"be6de9f.1769a18","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"5","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":649,"y":439,"wires":[["dcc26f36.fa1ac","cdf18bca.67c328"]]},{"id":"27d4d8db.547278","type":"tcp out","z":"be6de9f.1769a18","host":"","port":"","beserver":"reply","base64":false,"end":false,"name":"","x":995,"y":511,"wires":[]},{"id":"cdf18bca.67c328","type":"function","z":"be6de9f.1769a18","name":"","func":"msg.payload=\"Awesome! got it\";\nreturn msg;","outputs":1,"noerr":0,"x":840.5,"y":509,"wires":[["27d4d8db.547278"]]},{"id":"1ea6e66e.e380ba","type":"ui_group","z":"","name":"TCP RAW Data","tab":"115d9936.2b4ef7","disp":true,"width":"20"},{"id":"115d9936.2b4ef7","type":"ui_tab","z":"","name":"Sensor Data","icon":"dashboard","order":3}]

I've a cellular based module with my Arduino which can send both TCP and UDP data. What I'm trying to do is to:
1) Visualize the data from sent by the Arduino + Cellular module
2) Store the data 
3) Acknowledge upon receiving the data (both TCP and UDP)

I'm stuck at Point #3 UDP 2 way communication. I can send data from my Arduino + Cellular module, receive it in Node-RED, However, when I use the above flow to send an Acknowledgement back to the module, it won't send. Similar settings works well on TCP. 

Nick O'Leary

unread,
Sep 26, 2017, 5:49:40 AM9/26/17
to Node-RED Mailing List
Only immediate suggestion I have is to untick the 'Decode Base64 encoded payload` option in the UDP Out node - as the payload you're passing it isn't base64.

Nick

To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Ram

unread,
Sep 26, 2017, 6:50:29 AM9/26/17
to Node-RED
Done. Still couldn't get it to work. :(

Dave C-J

unread,
Sep 26, 2017, 11:18:16 AM9/26/17
to node...@googlegroups.com
you may also need to bind to local port 5121 if the device is expecting a reply back from that same port - otherwise it will pick a random local port and send from that. You need to understand what the remote device needs exactly.

Ram

unread,
Sep 26, 2017, 8:39:21 PM9/26/17
to Node-RED
Awesome! That actually fixed my issue. Thanks a lot :)
Reply all
Reply to author
Forward
0 new messages