# UDP out node set msg.localport

**URL:** <https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009>\
**Category:** General\
**Tags:** function-node, udp\
**Created:** [29 January 2024 06:38 UTC](https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009 "2024-01-29T06:38:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgangdfp](https://avatars.discourse-cdn.com/v4/letter/m/ba9def/32.png) [@morgangdfp](https://discourse.nodered.org/u/morgangdfp)\
**Post date:** [29 January 2024 06:38 UTC](https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009/1 "2024-01-29T06:38:45Z")

</div>

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:

```auto
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](https://discourse.nodered.org/t/udp-out-node-bind-to-local-port/53636) post but I don't understand and if I try to code I get this error: `TypeError: Cannot read properties of undefined (reading 'createSocket')`

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [29 January 2024 07:35 UTC](https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009/2 "2024-01-29T07:35:44Z")

</div>

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

```auto
  /** 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....

```auto
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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 January 2024 08:01 UTC](https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009/3 "2024-01-29T08:01:04Z")

</div>

> [@marcus-j-davies](#):
>
> `dgram`

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 April 2024 08:01 UTC](https://discourse.nodered.org/t/udp-out-node-set-msg-localport/85009/4 "2024-04-28T08:01:17Z")

</div>

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