# \[ANNOUNCE\] Visual programming a function node with Blockly - Feedback request

**URL:** <https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566>\
**Category:** Share Your Nodes\
**Created:** [13 July 2018 22:33 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566 "2018-07-13T22:33:30Z")\
**Posts on this page:** 20\
**Page:** 7

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [15 August 2018 22:29 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/122 "2018-08-15T22:29:08Z")

</div>

Simon,

There is a new version on Github with following fixes:

- Status block: it now generates **yellow** instead of orange (see animation below).
- Status block: if the **status text is empty** , it now generates a status object **{}** to remove the status from the node (see animation below). This way this weird error is not displayed anymore:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4e6beceda4e5ea94cd2f314a742e9e54baac4160.png)
- Both the javascript files (for Blockly and all blocks) and the xml files (for toolboxes) are now loaded from a **relative server path**. You shouldn't notice this, but there was an [issue](https://github.com/bartbutenaers/node-red-contrib-blockly/issues/12) reported that the Blockly editor wasn't visible when _httpAdminRoot_ was used.
- **Extra logging** is added for the toolbox XML document creation. This seems to fail (see also [issue](https://github.com/bartbutenaers/node-red-contrib-blockly/issues/12)) for that user, but I don't have clue at the moment why that happens. But again you shouldn't notice a difference ...

This animation shows the yellow status displaying, and how to remove the node status:

 ![blockly_status_remove](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/e55fe5672acda8a3f5ddef8309d6da3f0dc0d5f8.gif)

P.S. instead of adding a text-block without text, it now also works when no text-block is added:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/db4c7241ba58dafbdff4198880b832a9ff006a64.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 07:34 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/123 "2018-08-16T07:34:58Z")

</div>

Works lovely now 🙂  
 ![GIF](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/14ea543d066af75d3a2e4ec7e75cc8d95fca1d94.gif)

I MUST STOP SUGGESTING THINGS

but ......

maybe the status block should have a default empty text block instead of an empty space and there should be an explicit clear node status block?

Simon (running away fast......................................................... )

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 09:41 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/124 "2018-08-16T09:41:11Z")

</div>

[BUG]  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d4e885401961ff8cb94265430cd046efe3675856.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/e/ea9382e0d321d5bd5b5c1a62d2b611c5dff7340b.png)

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 August 2018 20:44 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/125 "2018-08-16T20:44:34Z")

</div>

Simon,

A new version is available on Github to fit your needs:

- Your pull request has been merged, so the orange color (in the color picker) is changed to **yellow** :  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/074893e5cf994a2fc6dda898ede6433943b1a2a6.png)

- The status node has now a **default empty text** field (instead of no text field):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7e58491f468783d2967396413425e3a4d0bce92f.png)

- I have added a new ' **remove node status**' block:  

- The **bug** about the non-existing output number is fixed. Problem was that I choosed at generation time which code I had to generate: the code with msg.send ... OR the code with node.error ... But that was incorrect in your case: you used a variable (which can change at runtime), so at generation time I cannot always determine which port number will be used.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 20:46 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/126 "2018-08-16T20:46:35Z")

</div>

re Bug above  
That's been fixed 🙂  
But still not quite right

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/7abb05c401059a92474491415ac41945843e3651.png)

```
var outputPort;

function isValidOutput(outputNumber) {
  const AVAILABLE_OUTPUTS = 2;
  if (outputNumber > AVAILABLE_OUTPUTS) {
    node.error("Cannot send to output outputPort, since only 2 outputs available");
    return false;
  }
  else {
    return true;
  }
}

outputPort = 1;
if ((msg['payload']) == true) {
  outputPort = 2;
}
if(isValidOutput(outputPort)) {
  node.send([null, null]);
} 

```

I think it should end up issuing something like  
node.send([null,msg]);

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 20:47 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/127 "2018-08-16T20:47:28Z")

</div>

I must have pressed button at same time as you

Let me read yours 1st 🙂

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 20:55 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/128 "2018-08-16T20:55:18Z")

</div>

mm...I see its complicated ☹

If you can't see how to make it work at the moment then maybe remove ability to have more than 1 output at the moment.

Add it back in a later release?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [16 August 2018 21:04 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/129 "2018-08-16T21:04:30Z")

</div>

I'm currently using this as a workaround for the moment

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/1b960d7caa0981a16df177c5f7247037c7c27a51.png)

```
var outputPort;

function isValidOutput(outputNumber) {
  const AVAILABLE_OUTPUTS = 2;
  if (outputNumber > AVAILABLE_OUTPUTS) {
    node.error("Cannot send to output 1, since only 2 outputs available");
    return false;
  }
  else {
    return true;
  }
}

outputPort = 1;
if ((msg['payload']) == true) {
  outputPort = 2;
}
if (outputPort == 1) {
  if(isValidOutput(1)) {
    node.send([msg, null]);
  }
} else {
  if(isValidOutput(2)) {
    node.send([null, msg]);
  }
}

```

which is good enough for me at moment 🙂  
Simon

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 August 2018 21:07 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/130 "2018-08-16T21:07:27Z")

</div>

Damn again... I have currently a loop inside the code generator, to _ **build the node.send statement** _:

```auto
    // Send the message only to the specified output number, and null to all other outputs.
    var outputArray = '';
    for (var i = 1; i <= Blockly.nodeOutputs; i++) {
        if (i > 1) {
            outputArray += `, `;
        }
        
        if (i == outputNumber) {
            outputArray += `${inputValue}`;
        }
        else {
            outputArray += `null`;
        }
    }
    
    var code = `if(${functionName}(${outputNumber})) {\n`;
    code += ` node.send([${outputArray}]);\n`;
    code += `}\n`;

```

But since I don't know anymore at code-generation-time which output number will be used (since that is only determined at run-time), this means I have to put the above loop also in the generated Javascript. This will result in unreadable code. Don't like that. It should stay in the code generator, and be invisible to the users.

Isn't it better that we don't allow the output number to be generated at runtime. I.e. **only allow a fixed number to be entered**. _ **The logic to determine which output needs to be used shouldn't be INSIDE the output\_number input field, but instead AROUND our send-block** _. For example by using an if-block:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/ffbc70fd15a43f087e634236c02fdc924a7450e0.png)

Otherwise nobody will understand the generated code anymore. Does this makes sense?  
@TotallyInformation: Julian, would be nice to get your opinion. And also other users ...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 August 2018 21:08 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/131 "2018-08-16T21:08:22Z")

</div>

Now I saw your latest post too late. Seems my last proposal is the same as your workaround ...

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [16 August 2018 22:05 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/132 "2018-08-16T22:05:06Z")

</div>

I reworked the send block to work again with fixed output numbers.

In the following example I have a blockly node with 3 outputs.

- Messages with payload '1' will be send to output 1
- Messages with payload '2' will be send to output 2
- Messages with payload '3' will be send to output 3
- Messages with payload '4' will result in an error

![blockly_send_fixed](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/859c24ac21fdd79a99e96cea8daa5bece1cbf2ab.gif)

I think this enough for most users. It generates very compact understandable Javascript code, and the users are still able to do some more advanced stuff (like the message routing in the above animation).

P.S. If no counter proposal, I will put this version on Github tomorrow evening.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 August 2018 08:05 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/133 "2018-08-17T08:05:10Z")

</div>

> [@BartButenaers](#):
>
> P.S. If no counter proposal, I will put this version on Github tomorrow evening.

My counter proposal is to allow arrays to be sent instead of just msg

so have a block  
`send xxx to output`  
where xxx defaults to msg

but let people set xxx to `[msg,null]`  
or whatever they want

This would align it perfectly with JS

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [17 August 2018 08:26 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/134 "2018-08-17T08:26:57Z")

</div>

Since there are e.g. multiple LOOP blocks available, we might offer two separate SEND blocks:

- one with an msg and an output number
- one with a msg array

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 August 2018 08:54 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/135 "2018-08-17T08:54:07Z")

</div>

Not sure what you mean by "Since there are e.g. multiple LOOP blocks available,"

I'm off out now till 16:00 GMT - see you later 🙂

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [17 August 2018 14:52 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/136 "2018-08-17T14:52:02Z")

</div>

I just wanted to say that Google also provides sometimes multiple blocks to achieve the same result. For example when you want to repeat something 10 times, they offer two blocks to do this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/c/cd828ab6cd4db9319a264dd932c7b4989d70e0b1.png)

So we can also offer two separate blocks for sending messages: one that accepts a list and another that accepts an output number. That's all ...

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 August 2018 16:30 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/137 "2018-08-17T16:30:42Z")

</div>

understood

---

<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 August 2018 18:56 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/138 "2018-08-17T18:56:43Z")

</div>

Sorry been busy catching up work.

In regard to having multiple output channels, the Node-RED way is to define the number in the configuration of the node instance. This makes it fixed and therefore known in advance doesn't it? Or have I missed something? Maybe someone could drop in an example flow to illustrate the issue?

In regard to having multiple blocks to do something similar, I think this is absolutely in line with the expected audiences. Having a simplified block and one that is more complex but flexible. As long as it doesn't get out of hand so that you end up with thousands 😏

One small thing about code style - you seem to have a lot of back-ticks rather than the single quotes (or double if including escaped control characters) I would generally use - is there a reason for that? I'm not sure if there is a performance or other impact from only using back-ticks?

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 August 2018 19:25 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/139 "2018-08-17T19:25:58Z")

</div>

> [@TotallyInformation](#):
>
> This makes it fixed and therefore known in advance doesn't it? Or have I missed something? Maybe someone could drop in an example flow to illustrate the issue?

> [@\[ANNOUNCE\] Visual programming a function node with Blockly - Feedback request](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/126):
>
> re Bug above That's been fixed slight_smile But still not quite right [image] var outputPort; function isValidOutput(outputNumber) { const AVAILABLE\_OUTPUTS = 2; if (outputNumber \> AVAILABLE\_OUTPUTS) { node.error("Cannot send to output outputPort, since only 2 outputs available"); return false; } else { return true; } } outputPort = 1; if ((msg['payload']) == true) { outputPort = 2; } if(isValidOutput(outputPort)) { node.send([null, null]); } I think it sho…

The "problem" is that , at present moment, the Blockly node lets me use a variable for the ouput.  
But since the value of the variable is not known at "compile time" (when the JS is generated) then trying to cope with that situation leads to a lot of complex JS (that Bart is trying to avoid)

> [@\[ANNOUNCE\] Visual programming a function node with Blockly - Feedback request](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/130):
>
> Damn again... I have currently a loop inside the code generator, to build the node.send statement: // Send the message only to the specified output number, and null to all other outputs. var outputArray = ''; for (var i = 1; i \<= Blockly.nodeOutputs; i++) { if (i \> 1) { outputArray += `, `; } if (i == outputNumber) { outputArray += `${inputValue}`; } else { outputArray += `null`; } } …

(Follow the links to previous posts - forum doesn't format them properly when I pasted the link in!)

---

<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 August 2018 19:33 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/140 "2018-08-17T19:33:08Z")

</div>

But can't you simply fix the number of outputs in advance like you would have to in a function node?

Sorry if I'm being thick, it's been a long week and I spent the whole afternoon in a remote workshop designing a data model - brain melt!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [17 August 2018 19:49 UTC](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566/141 "2018-08-17T19:49:02Z")

</div>

> [@TotallyInformation](#):
>
> But can't you simply fix the number of outputs in advance like you would have to in a function node?

Yes AND No - think Schrodinger's Cat Problem 🙂

> [@TotallyInformation](#):
>
> it's been a long week and I spent the whole afternoon in a remote workshop designing a data model - brain melt!

The issue is not for those suffering from withered brain cells 🙂

[Previous page](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566.md?page=6)

[Next page](https://discourse.nodered.org/t/announce-visual-programming-a-function-node-with-blockly-feedback-request/1566.md?page=8)
