UDP out node set msg.localport

I am running node red on an ubuntu server and I have configured one of the network adapters to have several IP addresses, like this:

network:
  ethernets:
    enp0s3:
      dhcp4: true
    enp0s8:
       dhcp4: no
       addresses:
         - 192.168.25.9/24
         - 192.168.25.23/24
         - 192.168.25.43/24
         - 192.168.25.45/24
         - 192.168.25.67/24
  version: 2

The idea is that I want to use node red to simulate several controllers, the different IP addresses corresponds to the controllers IP addresses.

In node red I am using an UDP out node to send packets and respond to heartbeat communication but I have not figured out a way to specifically send packets from for example IP 192.168.25.43. I have read something about using the bind call to specify source address but I have not managed to figure out how it works.

I found this post but I don't understand and if I try to code I get this error: TypeError: Cannot read properties of undefined (reading 'createSocket')

The post you're referring to, fails to mention the need to expose the dgram module to function nodes

You do this in settings.js of Node RED

  /** The following property can be used to set predefined values in Global Context.
     * This allows extra node modules to be made available with in Function node.
     * For example, the following:
     *    functionGlobalContext: { os:require('os') }
     * will allow the `os` module to be accessed in a Function node using:
     *    global.get("os")
     */
    functionGlobalContext: {
        // os:require('os'),
    },

So for you....

functionGlobalContext: {
   dgram:require('dgram')
},

You can then use the code snippet from that post in function nodes.
Remember to restart Node RED after modifying the settings file.

1 Like

Much easier to add dgram to the function node setup tab these days.

2 Likes

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