nofloFlow-Based Programming environment for Node.js Version0.0.2 Author
ContributorsJerry Jalava Ryan Shaw Repositorygit - git://github.com/bergie/noflo.git Dependencies
Statshttp://cloc.sourceforge.net v 1.55 T=1.0 s (71.0 files/s, 3634.0 lines/s) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- CoffeeScript 71 656 228 2750 ------------------------------------------------------------------------------- SUM: 71 656 228 2750 ------------------------------------------------------------------------------- | NoFlo: Flow-based programming for Node.jsNoFlo is a simple flow-based programming implementation for Node.js. From WikiPedia:
Developers used to the Unix philosophy should be immediately familiar with FBP:
It also fits well in Alan Kay's original idea of object-oriented programming:
NoFlo has been written in CoffeeScript for simplicity. The system is heavily inspired by J. Paul Morrison's book Flow-Based Programming. Currently NoFlo is still in quite early stages. It has already been used in some real-world applications, but the small number of available components still limits the utility of the system. Requirements and installingNoFlo requires a reasonably recent version of Node.js, and some npm packages. Ensure you have the
Then you can install everything needed by a simple:
NoFlo is available from GitHub under the MIT license. Using NoFloThere are two ways to use NoFlo:
When you create a NoFlo graph, it doesn't do anything by itself. It only loads the components of the graph and sets up the connections between them. Then it is up to the components to actually start sending messages to their outports, or reacting to messages they receive on their inports. Since most components require some input before they act, the usual way to make a NoFlo graph run is to send it some initial information packets, or IIPs. Examples of this would include sending a port number that a web server could listen to the web server component, or sending a file name to a file reader. This activation model provides many possibilities:
Running the examplesFile line count using embedded NoFlo:
File line count as an individual NoFlo application:
or
Simple "Hello, world" web service with Basic authentication using embedded NoFlo:
Then just point your browser to http://localhost:8003/. Note that this example needs to have Terminology
ComponentsA component is the main ingredient of flow-based programming. Component is a CommonJS module providing a set of input and output port handlers. These ports are used for connecting components to each other. NoFlo processes (the boxes of a flow graph) are instances of a component, with the graph controlling connections between ports of components. Structure of a componentFunctionality a component provides:
Minimal component written in CoffeeScript would look like the following:
This example component register two ports: in and out. When it receives data in the in port, it opens the out port and sends the same data there. When the in connection closes, it will also close the out connection. So basically this component would be a simple repeater. You can find more examples of components in the SubgraphsA NoFlo graph may contain multiple subgraphs, managed by instances of the The Graph component loads the graph given to it as a new NoFlo network, and looks for unattached ports in it. It then exposes these ports as its own inports or outports. This way a graph containing subgraphs can easily connect data between the main graph and the subgraph. Unattached ports from the subgraph will be available through naming Simple example, specifying what file a spreadsheet-parsing subgraph should run with:
Some words on component designComponents should aim to be reusable, to do one thing and do it well. This is why often it is a good idea to split functionality traditionally done in one function to multiple components. For example, counting lines in a text file could happen in the following way:
This way the whole logic of the application is in the graph, in how the components are wired together. And each of the components is easily reusable for other purposes. If a component requires configuration, the good approach is to set sensible defaults in the component, and to allow them to be overridden via an input port. This method of configuration allows the settings to be kept in the graph itself, or for example to be read from a file or database, depending on the needs of the application. The components should not depend on a particular global state, either, but instead attempt to keep the input and output ports their sole interface to the external world. There may be some exceptions, like a component that listens for HTTP requests or Redis pub-sub messages, but even in these cases the server, or subscription should be set up by the component itself. When discussing how to solve the unnecessary complexity of software, Out of the Tar Pit promotes an approach quite similar to the one discussed here:
Done this way, components represent the pure logic, and the control flow and state of the application is managed separately of them in the graph. This separation makes the system a lot simpler. Ports and eventsBeing a flow-based programming environment, the main action in NoFlo happens through ports and their connections. There are several events that can be associated with ports:
It depends on the nature of the component how these events may be handled. Most typical components do operations on a whole transmission, meaning that they should wait for the disconnect event on inports before they act, but some components can also act on single data packets coming in. When a port has no connections, meaning that it was initialized without a connection, or a detach event has happened, it should do no operations regarding that port. The NoFlo shellNoFlo comes with a command shell that you can use to load, run and manipulate NoFlo graphs. For example, the line count graph that was explained in Component design could be built with the shell in the following way:
You can run help to see all available NoFlo shell commands, and quit to get out of the shell. Designing NoFlo graphs with DrawFBPAs of version 2.6 onwards, the DrawFBP GUI tool for designing Flow-Based Programming graphs is able to generate graphs compatible with NoFlo. The graphs can be exported to NoFlo format from the File -> Generate network -> NoFlo menu and then run normally. The web-based NoFlo monitorIn addition to the shell, NoFlo also comes with a web interface that allows loaded graphs to be monitored. To start it, load a graph into the NoFlo shell, and run:
This will start the NoFlo monitor on port At the moment the monitor only displays the graph, showing the processes and connections between them. Real-time statistics of data flow, and support for visual graph editing are planned. NoFlo graph file formatIn addition to using NoFlo in embedded mode where you create the FBP graph programmatically (see example), you can also initialize and run graphs defined using a JSON file. The NoFlo JSON files declare the processes used in the FBP graph, and the connections between them. They look like the following:
To run a graph file, you can either use the load command of the NoFlo shell, or do it programmatically:
Language for Flow-Based ProgrammingIn addition to the JSON format described above, FBP has its own Domain-Specific Language (DSL) for easy graph definition. The syntax is the following:
You can connect multiple components and ports together on one line, and separate connection definitions with a newline or a comma ( Components only have to be specified the first time you mention a new process. Afterwards, simply append empty parentheses ( Example:
NoFlo supports the FBP language fully. You can either load a graph with a string of FBP-language commands with:
The
DevelopmentNoFlo development happens on GitHub. Just fork the main repository, make modifications and send a pull request. To run the unit tests you need nodeunit. Run the tests with:
or:
DiscussionFlow-based programming in general, including NoFlo can be discussed on the Flow Based Programming Google group. There is also an IRC channel Some ideas
|