# Node.send() vs send() – what's the difference?

**URL:** <https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811>\
**Category:** Developing Nodes\
**Created:** [7 July 2022 23:05 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811 "2022-07-07T23:05:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![phoddie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phoddie/32/64232_2.png) [@phoddie](https://discourse.nodered.org/u/phoddie)\
**Post date:** [7 July 2022 23:05 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811/1 "2022-07-07T23:05:58Z")

</div>

I'm trying to understand the difference between using `node.send()` and `send()` in a node. This is for the Node-RED MCU Edition exploration, to be able to properly enumerate the full Node-RED behavior. when using the CompatibilityNodeto run nodes.

The "[JavaScript file](https://nodered.org/docs/creating-nodes/node-js#sending-messages)" document Sending Messages section says:

> If the node wants to send from inside the `input` event listener, in response to receiving a message, it should use the `send` function that is passed to the listener function:

On the page "[Creating your first node](https://nodered.org/docs/creating-nodes/first-node#lower-casejs)", the example calls `node.send()` instead of `send()` from within the `input` event listener.

```javascript
node.on('input', function(msg) {
      msg.payload = msg.payload.toLowerCase();
      node.send(msg);
});

```

This seems to contradict the "JavaScript file" document which suggests writing it this way:

```javascript
node.on('input', function(msg, send) {
      msg.payload = msg.payload.toLowerCase();
      send(msg);
});

```

Finally, the "Writing Functions" page under "Sending messages asynchronously" uses `node.send()` and it appears this is the only option as there is no `send` available to `"On Message"`.

Apart from the note in "Sending Messages" in the "JavaScript file" document, it appears acceptable to use `node.send()` everywhere. What is different about using `send()`?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [8 July 2022 06:58 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811/2 "2022-07-08T06:58:41Z")

</div>

Hi @phoddie

This relates to your previous questions about `done` and the Complete node.

The preferred way is to use `send()` as that allows the runtime to correlate which arriving message triggered the send.

This blog post explains the difference when it was first introduced: [Knowing when a node is done : Node-RED](https://nodered.org/blog/2019/09/20/node-done)

---

<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:** [8 July 2022 07:21 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811/3 "2022-07-08T07:21:29Z")

</div>

> [@phoddie](#):
>
> I'm trying to understand the difference between using `node.send()` and `send()` in a node

In the context of a custom (add-in) node, the end result is the same.

The "JavaScript file" docs are more current: If the node wants to send from inside the on `input` event listener, in response to receiving a message, it should use the `send` function that is passed to the listener function:

This is the "newer" api that supports the complete node via the `done` callback.

> ```auto
> let node = this;
> this.on('input', function(msg, send, done) {
> // For maximum backwards compatibility, check that send exists.
> // If this node is installed in Node-RED 0.x, it will need to
> // fallback to using `node.send`
> send = send || function() { node.send.apply(node,arguments) }
> 
> msg.payload = "hi";
> send(msg);
> 
> if (done) {
> done();
> }
> });
> 
> ```

  

The issue (in the case of mcu) may be that some contrib nodes (and indeed some of the older built in nodes) have not yet been updated to the new API so there is a mix throughout the ecosystem.

In essence they are equivalents

- `node.send(msg)` == `send(msg)`
- `node.error(err, msg)` == `done(err)`

I am not certain how or if this will have any bearing or impact on design of mcu at this point (would need a use case)

> [@phoddie](#):
>
> Apart from the note in "Sending Messages" in the "JavaScript file" document, it appears acceptable to use `node.send()` everywhere. What is different about using `send()`?

Some nodes do not have an input & therefore no `(send, done)` functions passed in. In these cases `node.send` and `node.error` are the only option.

For some nodes, it may not make sense to utilise the `complete` node (e.g. a cron type node with an endless pulse is never really complete)

  

> [@phoddie](#):
>
> Finally, the "Writing Functions" page under "Sending messages asynchronously" uses `node.send()` and it appears this is the only option as there is no `send` available to `"On Message"`.

That is correct.  
An important aspect to the function node is that it is executed in a VM sandbox and is isolated from the actual runtime. The `send` and `done` functions are mapped to an object named `node` along with some other properties. To be clear, the function code entered by a user is wrapped as below...

```javascript
var results = null; results = (async function (msg, __send__ , __done__ ) {
    var __msgid__ = msg._msgid; 
    var node = { id: __node__.id, name: __node__.name, path: __node__.path, outputCount: __node__.outputCount, log: __node__.log, error: __node__.error, warn: __node__.warn, debug: __node__.debug, trace: __node__.trace, on: __node__.on, status: __node__.status, send: function (msgs, cloneMsg) { __node__.send( __send__ , __msgid__ , msgs, cloneMsg); }, done: __done__ };
    // user code start
    msg.payload = new Date().toString()
    node.send(msg)
    node.done()
    // user code end
})(msg, __send__ , __done__ );

```

* * *

Other info of importance as you explore the MCU implementation...

- [Making flows asynchronous by default : Node-RED](https://nodered.org/blog/2019/08/16/going-async)
- [Cloning messages in a flow : Node-RED](https://nodered.org/blog/2019/09/13/cloning-messages) - relevant to function nodes `node.send` ~ vs ~ a custom node (in the runtime) `node.send` behaviour.

* * *

Hope these disjointed replies are of some use.

PS, keep up the great work 👍

---

<div class="post-metadata">

**Author:** ![phoddie](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/phoddie/32/64232_2.png) [@phoddie](https://discourse.nodered.org/u/phoddie)\
**Post date:** [9 July 2022 01:47 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811/4 "2022-07-09T01:47:20Z")

</div>

@Steve-Mcl and @knolleary – thank you both **very much** for your replies. They definitely help. I think I'm starting to understand. I need a little more time to think and experiment before asking more about this topic.

FWIW, most of Node-RED has been remarkably straightforward to follow. Every large system has some complex parts; `send` and `done` seem to be that for Node-RED. 😉 From your comments, part of that is to maintain backwards compatibility.

---

<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:** [7 September 2022 01:48 UTC](https://discourse.nodered.org/t/node-send-vs-send-whats-the-difference/64811/5 "2022-09-07T01:48:16Z")

</div>

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