# Fork Node or Clone Node?

**URL:** <https://discourse.nodered.org/t/fork-node-or-clone-node/81389>\
**Category:** General\
**Created:** [17 September 2023 08:41 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389 "2023-09-17T08:41:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [17 September 2023 08:41 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/1 "2023-09-17T08:41:42Z")

</div>

Hi There!

I'm doing some UML2Node-RED flows and I came across a UML node that Node-RED doesn't seem to have and that is the fork node. A fork node receives one message and clones that message to each of it's output ports. Is there a an equivalent node in Node-RED?

I simulated the node using a function node with 4 outputs (because I needed 4 outs) and the following code:

```javascript
return [
    { ...msg },
    { ...msg },
    { ...msg },
    { ...msg }
];

```

definitely not rocket science!

An alternative would be to use some sort of clone node (i.e. duplicate a message X times into an array) and then use the split node. The individual messages _won't_ be the same since the split node assigns a `parts` attribute to the `msg` object, but that can be ignored.

Am I overseeing something and there is a fork node?

Cheers!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2023 08:52 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/2 "2023-09-17T08:52:15Z")

</div>

Reading this, would just adding four wires to the function output create four cloned messages?

> **[Cloning messages in a flow](https://nodered.org/blog/2019/09/13/cloning-messages)**
>
> With the move to asynchronous messaging, we're also changing how the Function node clones messages. Find out what cloning is all about, why it's necessary and what's changing in 1.0.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [17 September 2023 09:07 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/3 "2023-09-17T09:07:12Z")

</div>

Ah of course, what am I saying! I think my brain had a meltdown - it's just four wires from a node - doh!

Ok, sorry for my sunday morning meltdown 🤪

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [17 September 2023 12:54 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/4 "2023-09-17T12:54:32Z")

</div>

@gregorius The good news is that you helped me to avoid cloning. I am working on a flow that is sending much pixel buffer data over websockets and forgot to add the `false` in `node.send()`. This could have been a costly oversight on my part.

@E1cid Thanks for the link to inform us. It seems that it does not explain that using `return msg` will or will not clone the first message, as was [resolved here](https://discourse.nodered.org/t/avoiding-cloning-messages/24808/4).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [17 September 2023 13:18 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/5 "2023-09-17T13:18:34Z")

</div>

> [@kevinGodell](#):
>
> `return msg` will or will not clone the first message, as was [resolved here](https://discourse.nodered.org/t/avoiding-cloning-messages/24808/4)

Maybe i should of worded my post , `would just adding four wires to the function output create the OG message plus three cloned messages?` that may have been clearer. But cheers for clarification for future readers.

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [17 September 2023 14:10 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/6 "2023-09-17T14:10:36Z")

</div>

I also create a [Node-RED learning](https://flowhub.org/f/36f1ec196998b047) out of this meltdown, so it did have a happy end in the end!

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 September 2023 18:12 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/7 "2023-09-17T18:12:21Z")

</div>

Ah, but .... 🙂

You didn't mention shallow vs deep cloning.

By default, JavaScript will only ever shallow clone as far as I know. To deep clone requires something extra. Which is why Node-RED has a utility function to do just that.

[Deep copy - MDN Web Docs Glossary: Definitions of Web-related terms | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy)

`RED.util.cloneMessage(..)` : safely clones a message object so it can be reused

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [17 September 2023 20:30 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/8 "2023-09-17T20:30:52Z")

</div>

> [@TotallyInformation](#):
>
> RED.util.cloneMessage(..)

Ah thanks for that, I forgot about that - [flow updated](https://flowhub.org/f/36f1ec196998b047?v2=f83cd8fb9fed555bfea103d83b0323288c0b197f&v1=3b7ee6337da077e211563341d4cc53b1e089f591)

---

<div class="post-metadata">

**Author:** ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)\
**Post date:** [18 September 2023 10:23 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/9 "2023-09-18T10:23:55Z")

</div>

One last thing to mention is [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone). I used it recently on some client side js in a web component, but it is also available on the server side as of node.js v17. It seemed to work ok. Sounds like something that can be tested/compared.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [18 September 2023 16:56 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/10 "2023-09-18T16:56:19Z")

</div>

Certainly something that should be revisited once Node-RED hits node.js v18 as its minimum requirement.

Also, should only be used in the browser if you are sure you will only ever have browsers newer than early 2022 (Safari got this in March 2022, Chrome/Edge in Feb 2022, Firefox in Nov 2021).

---

<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:** [2 October 2023 16:56 UTC](https://discourse.nodered.org/t/fork-node-or-clone-node/81389/11 "2023-10-02T16:56:29Z")

</div>

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