Logical choreography is used to describe logic in a visual way and is used to replace the logical description part in general construction scenes.

Node-red is a paAS tool that fully realizes logic orchestration. This week’s in-depth reading content is its introduction video: How To Create Your First Flow In Node-Red describes How To implement a weather query application using pure logical orchestration, as well as deployment and application migration.

An overview of the

Running Node-Red locally is as simple as using the following two commands:

npm install -g --unsafe-perm node-red
node-red
Copy the code

After that, you can see the logic layout interface:

These logical nodes can be used to build front-end websites, back-end services, and most of the development work. While this is a bit abstract, we’ll go through the role of each logical node to give you an idea of how these logical nodes are planned and designed, and how logic choreography controls development practices to improve development efficiency.

Node-red currently has 42 logical nodes, which are divided into six categories according to general purpose, function, network, sequence, parsing and storage.

All nodes may have left and right join points. The left join point is the input and the right join point is the output. A particular node may have multiple inputs or outputs.

The following describes the functions of each node in turn.

general

Common nodes handle common logic, such as manual data entry, debugging, error capture, annotations, and so on.

inject

Manually enter the node. Periodically, some input can be generated and consumed by the next node.

For example, it is possible to generate fixed values periodically, such as this object:

return {
  payload: new Date(),
  topic: "abc"};Copy the code

Of course, this is configured with a UI form:

After that comes consumption, which can be consumed by almost any node, such as using the change node to set some environment variables, or using the template node to set the HTML template. In templates, variables are accessed through {{MSG. Payload}}, and in other forms, you can even enumerate the selection directly through the drop-down box.

While this node is often used to set static variables, more often the input comes from other programs or users, such as HTTP in, which will be covered later. We can mock this by replacing the input of any node from the production node with the inject node, which also supports the configuration of timed automatic firing:

debug

For debugging, when any output node is connected to the debug input, the output will be printed on the console for easy debugging.

For example, if we connect the input of Inject to the input of debug, we can see the print result on the console after the data is triggered:

Of course, if you connect the input to debug, the original logic is broken, but any output node can output to other nodes without limit, you just need to connect the output to the debug and function nodes at the same time:

complete

Listen for some nodes to trigger the completion action. With this node, we can capture actions triggered by any node, plug in a Debug node to print logs, or a function node to handle logic.

You can listen on all nodes, or visually select which nodes to listen on:

catch

Error capture node, output when any or a specified node triggers an error, output in the format:

Error. message Indicates a string error message. Error.source. id String Indicates the ID of the node from which the error was raised. Error.source. type The type of the node from which the error was raised. Error.source. name The name of the node from which the error was raised. (If set)Copy the code

In fact, each node has a fixed output format. These fixed formats limit the flexibility of development, but mastering them can greatly improve development efficiency, because all nodes of the same type have the same format. This is the benefit of logical orchestration that brings rule constraints.

status

Listen for node status changes.

link in

Can only link out. Link in, Link out is like a portal that organizes logical orchestration nodes and makes them look easy to maintain.

For example, after a weather HTTP in service, there are many logical processing nodes, including the template node to process the HTML content, and the HTTP request service to process the request to query the city weather. Although the overall logic is aggregated, it is quite chaotic:

A better way is to classify, that is, similar to the modular behavior in code development, export the weather service, and import any other modules directly. This import action is achieved through link in, link out -> link in is only a spatial location transformation, transmission value is not changed:

This makes the module look much clearer, and if you want to know the connection between each “portal”, just click on one of them to give a hint, which looks very convenient:

link out

Link out can be used as a portal to transmit the value, without forming a visual connection line.

comment

Annotations, used in conjunction with the Link family, make the logical orchestration UI easier to maintain.

Combined with the example of the original video, for weather service, there is the logic of creating environment variables and the logic of querying, among which the query of weather can also be divided into querying the current weather, the weather for 5 consecutive days and the national information. We can divide each logical group on the UI and mark annotations with the comment component for easy reading:

function

Functional nodes, typically used to process business logic, contain basic if else, JS code, template processing, and other functional modules.

function

The core js function module, you can do anything with it:

Its input is transmitted to the MSG object, which can be modified by code and then transmitted out through the output node.

You can also access and modify nodes, processes, and global variables, as described in the change node.

switch

The switch code is just easier to use, because we can export different nodes according to different cases:

Notice in the figure above, since there are three branches, the node’s derived items also become three, and we can make different connections according to different logic:

change

Used to change environment variables. Environment variables fall into three categories: current node, process (canvas), and global (across applications). That is, variables can be stored on a node, on the entire canvas, or globally across canvases.

The access parameters are MSG., flow., and global. After setting these parameters, just like global variables, any node can be used anywhere, which is convenient.

For example, if an application has fixed URL addresses, it is not wise to write a string in an HTTP in node because it can be accessed by HTML or other nodes later on. Once you modify it, the influence is very wide, so it is best to set it as a global variable and access it through variables in the node:

In fact, on the console, you can see the values of these three variables:

When we assign with the change node, we can view the values of different scoped global variables in the debug panel:

range

Interval mapping, the mapping of values from one range to another. The function module can also do this, but because it is more commonly used, it encapsulates a special node. In fact, users can also encapsulate their own nodes. For details, please refer to the official documentation.

The figure above is easy to understand, such as data analysis normalization can be achieved using this node.

template

Generate a string or JSON as a template.

Function can also be replaced by function, but it is highlighted when used to write templates, so it is easier to maintain.

You have built-in mustache syntax for using variables in {{}}.

For example, we inject a variable into template and print it with debug. The process looks like this:

Inject is configured like this:

As you can see, set msg.name to a string and then access name via template:

delay

Delay sending messages, a shortcut tool that can be placed between any input and output. For example, in the example above, inject the result after 5s is triggered, it can be configured like this:

trigger

A message trigger, which provides more flexibility than Inject when to refire.

As you can see from the configuration, first send a message as inject, then can wait, either wait to be reset, or periodically trigger (so as inject), where “send a second message to a separate output” has an extra outlet like the Switch.

And then you have a reset condition, which is what the payload is.

As you can see from this component, each node can be implemented as a function node, but by customizing a node, it can be configured using UI rather than code, making it easier to use.

exec

Executing system commands, such as LS, which are executed in the background rather than the front end, is a rather dangerous node.

We can write any command in the configuration:

rbe

Report by Exception nodes, such as blocking when input changes.

network

It is used to create network services, such as HTTP, socket, TCP, UDP, and so on.

http in

Create an HTTP service, which can be any interface or Web service.

When you set Method to POST, connecting to HTTP Response creates a back-end interface; A Web service is created when it is set to a GET request, and the template is attached to write the HTML template, and the HTTP Response is attached.

Although it is difficult to create Web services in this way using the React or Vue frameworks, custom nodes do open up the possibility that you could actually define front-end modular files as nodes cascading together.

http response

HTTP return output, which can only interconnect with HTTP IN, is always used in pairs with HTTP IN.

If only HTTP in is used but HTTP Response is not used, the request is processed in the back-end code, but there is no call like this:

res.send("hello word");
Copy the code

Send content to the client.

http request

Unlike HTTP IN, which creates an HTTP service, HTTP Request sends a network request directly and imports the return value into the output node.

In this example, we use HTTP request to get weather information:

As you can see, after sending the request, the function node is used to process the return result. However, the use of function nodes is still expected to be limited in logical choreography, because unless they are well named, it is difficult to see the meaning of the nodes. If function handles too much or function blocks too many, logical choreography loses its meaning.

The sequence

A sequence is a node that processes an array.

split

Corresponds to the code split, which turns the string into an array.

join

The join of the corresponding code is generally used in conjunction with split to facilitate processing of strings.

sort

The corresponding code sort can only do simple ascending and descending processing according to the key, which is convenient for simple scenarios, but may also use function node instead for complex scenarios.

batch

After receiving input streams in batches, the input streams are packaged according to the quantity and then output in a unified manner, which is equivalent to batch packaging and can be grouped according to the quantity or time interval:

parsing

It is easy to understand how to specialize in processing data in the above formats and output it in terms of data characteristics, such as CSV data, which can be output as a single message per line, or packaged as a single message in a large array.

It can also be replaced by the function node, so that the parsing and output methods can be customized.

storage

Persistent storage, usually as files.

file

Output is a file.

file in

Take the file as input and the file result as output.

watch

Listen for changes to directories or files.

Intensive reading

After looking at the above Node-Red functionality, I believe that you have a more systematic understanding of logical orchestration.

The purpose of logical orchestration is to enable non-r&d people to quickly get started with the research and development work, so it is destined to serve the PAAS tool. Whether logical orchestration is good or not depends on whether the node functions are complete and whether the communication between nodes is smooth. For example, the Node-Red logical orchestration scheme is relatively mature in terms of completeness. It can be said that as long as you master a few core node rules, it is very efficient to use.

Logical orchestration also has a natural disadvantage. When all nodes are degraded to function nodes, there are two problems:

  • All nodes arefunctionNodes, even when stated, have very free internal implementation logic, resulting in a logical choreography that does not constrain input and output.
  • The regression to code functional invocation is essentially the same as writing code. Logic orchestration is efficient largely because I want the business logic to just match the function of the node, and it is efficient to implement it in a low-cost UI configuration.

This is also a solution, however, if your business logic layout cannot be existing node, you can try to abstract his teasing out business common node, and the reasonable configuration of encapsulation, as long as the common business logic can be encapsulated in the logical node, logical layout and effect for the business space.

conclusion

Logic choreography is one extreme, describing common business logic in a UI way that lowers the barriers for non-professional developers to get started. Analysis of Node-Red shows that a more complete logical orchestration system can still add value.

However, in view of the non-professional developers authors effect and a kind of extreme, is the complete code, but the code modular and function library, tool chain, and even low code platform construction is very complete, so that writing code efficiency is not low, this road to the extreme is good also, since want to in-depth development system, because spend time learning is the same, Why is learning to write code necessarily less efficient than learning to drag and drop UI? With highly encapsulated functions and tools, it’s no less efficient than UI drag and drop.

However, Node-Red can further enhance the template for creating front-end UI, upgrading the template from node to UI building canvas, logic orchestration is only used to process logic, so that the front-end development experience of large full-stack projects is better.

The discussion address is: Close reading low Code Logic Choreography · Issue #319 · dT-fe /weekly

If you’d like to participate in the discussion, pleaseClick here to, with a new theme every week, released on weekends or Mondays. Front end Intensive Reading – Helps you filter the right content.

Pay attention to the front end of intensive reading wechat public account

Copyright Notice: Freely reproduced – Non-commercial – Non-derivative – Remain signed (Creative Commons 3.0 License)