# nw_node

> NodeWire for nodejs

Latest version **1.2.7** (published 2020-09-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install nw_node
pnpm add nw_node
yarn add nw_node
bun add nw_node
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.7 |
| Published | 2020-09-11 |
| First published | 2018-07-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 55.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ahmad Sadiq |
| Maintainers | ahmadmicro |

## Links

- npm: https://www.npmjs.com/package/nw_node
- npm.io page: https://npm.io/package/nw_node

## Dependencies (2)

- [ws](https://npm.io/package/ws.md) ^5.2.2
- [lodash](https://npm.io/package/lodash.md) ^4.17.10

## Recent versions

- 1.2.7 (latest) — 2020-09-11
- 1.2.6 — 2020-08-22
- 1.2.5 — 2020-08-18
- 1.2.4 — 2020-08-18
- 1.2.3 — 2019-08-31
- 1.2.2 — 2019-07-30
- 1.2.1 — 2019-07-30
- 1.2.0 — 2019-07-30
- 1.1.9 — 2019-07-30
- 1.1.8 — 2019-07-30
- 1.1.7 — 2019-07-30
- 1.1.6 — 2019-07-30
- 1.1.5 — 2019-07-30
- 1.1.4 — 2019-06-29
- 1.1.3 — 2019-06-29
- … 13 more at https://npm.io/package/nw_node/versions

## README

Introduction
-------------
NodeWire is a framework for building IoT devices and software that works with them. This library allows you to build
nodejs applications that can seamlessly communicate with NodeWire based devices.

NodeWire supports a dataflow/message-driven programming paradigm. It extends the
reactive paradigm to distributed applications that are interconnected - also known as microservices. Every NodeWire app must implement a node interface. A node is a virtual appliance that has input and output ports. The inside of a node is a state machine and a node interacts with other nodes only through its input and output ports.

To read more about NodeWire please visit http://www.nodewire.org

Creating a node
-------------
First create an app:

```
$ mkdir mynode
$ cd mynode
$ npm init
 ```

Next, install nodewire:

```
$ npm install nw_node
```

Create your source file, 'app.js' and paste the following code in it:

```javascript
var nw = require('nw_node').nw;
var node = require('nw_node').node;

const conf = {                          // this will work but you should replace it with your own nodewire account credentials
    username:"test@microscale.net",    // register an account at dashboard.nodewire.org
    password:"aB01@",
    instance:"1jex2k7cbedg",
    server:"dashboard.nodewire.org",
}
nw.debug_level=0 // 0 = no debug messages, 1 = only important messages, 2 = all messages
nw.connect(conf);
nw.once('gack', async ()=>{
    let the_node = new node('test', {inputs: 'start reset', outputs: 'count'}); // create the node. the parameters aree the node name and the list of input and output ports

    the_node.count = 0; // initialize the port values, this is optional
    the_node.reset = 0;
    the_node.start = 0;
 
    the_node.when('reset', count=>the_node.count=count);  // respond to incoming port value

    setInterval(() => {            // the internal logic of our node: count if the start port is equal to 1
        if(the_node.start===1)
        {
            the_node.count += 1;
            console.log(the_node.count);
        }
    }, 1000);
});

```

Next run the code:

```
$ node app.js
```

Nothing significant will happen.

Now lets create another project, so we can control the node remotely:


Monitoring and Controlling the node
-------------------------------
Open another command/shell window and type the following:

```
$ mkdir myapp
$ cd myapp
$ npm init
 ```

Next, install nodewire:

```
$ npm install nw_node
```

Create your source file, 'app.js' and paste the following code in it:

```javascript
var nw = require('nw_node').nw;

const conf = {
    username:"test@microscale.net",
    password:"aB01@",
    instance:"1jex2k7cbedg",
    server:"dashboard.nodewire.org",
}
let thenode;
nw.debug_level=0 // 0 = no debug messages, 1 = only important messages, 2 = all messages
nw.connect(conf);
nw.once('gack', async ()=>{
    console.log('connected');
    thenode = await nw.getnode('test');  // get the node

    nw.when('test.count', (count)=>{  // monitor port changes
        console.log(count);
        if(count===10) thenode.reset = 20; // jump to count 20
        if(count==30){                // stop the count when count is 30
            thenode.start = 0;
            console.log('stop count');
        }
    });

    setTimeout(()=>{
        console.log('start count');
        thenode.start = 1;  // start count after 1 second
    }, 2000);
});

```

now, run application:

```
$ node app.js
```

You will the the two applications interacting with each other.

NodeWire Signals
----------------
NodeWire is based on an event driven paradigm. You always have to monitor signals in order to respond to them. And most of the code will be invalid if not executed after a certain event, most importantly after the 'gack' signal is received.

This signal signifies that the connection is fully established and all system parameters have been initialized.

Most signals are made of nodename followed by a period and then a portname: 'nodename.portname'. And it is activated whenever the corresponding portvalue changes.

There are two ways to monitor a signal, either by using the 'once' function which monitors the signal only once and then ignores subsequent occurrences. Or by using the 'when' function which continues to monitor the signal indefinitely. Both of these functions can be used simultaneously.


Controlling our node from the NodeWire dashboard
------------------------------------------------


Using the NodeWire Database to store values
------------------------------------------


Using NodeWire with ExpressJs
-----------------------------

---
_Source: https://npm.io/package/nw_node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
