Let's imagine what all devices in your house has it`s own name. You have a robot assistant, who is able to carry out your orders. His name is Victor. The kitchen has a fridge. His name is Good Boatswain. He keeps cold lemonade.
You ask Victor "Bring me lemonade"
Let's Express it in code.

Creating application

  • You need NodeJS https://nodejs.org
  • Create a project in your favorite IDE with two empty files victor.js и fridge.js
  • Run npm init in project directory to create package.json npm init
  • Run npm i intention-storage to install intention-storage package npm i intention-storage

In Intention Network each devices has intention storage. Let`s create intention storage for fridge

fridge.js const { IntentionStorage } = require('intention-storage');
// Create intention storage
const intentionStorage = new IntentionStorage();
// All storages can initiate connect with known storages, but not all storages can accept connections.
// If you want your device to accept connection, you need create server
// There can be one local intention server in your home that all device know. // And every device can be server by itself
const storageServer = intentionStorage.createServer({ address: 'localhost' });
// By default server will be create on port 10010

Now we create intention storage for Victor and ask him for lemonade.
Victor doesn`t have lemonade, that`s why he creates the intention.
Intention has input and output keys. It can be any strings. For example: "Lemonade - ThankYou".
Then input key is "Lemonade", and output key is our gratitude "ThankYou". When intention is created, the search of counter intention will starts
Counter intention is a intention with opposite order of input and output keys "ThankYou - Lemonade"
First will be search in the device`s local intention storage. If it has no results, then intention will be broadcasts to known storages. In our case it will be the fridge

victor.js const { IntentionStorage } = require('intention-storage');
const intentionStorage = new IntentionStorage();
// In the sake of simplicity let`s say Victor knows fridge
const link = intentionStorage.addStorage({ origin: 'localhost', port: 10010 });
link.connect();
// Victor creates intention "Need lemonade"
intentionStorage.createIntention({
  title: {
    en: 'Need lemonade'
  },
  input: 'Lemonade',
  output: 'ThankYou',
  onData: onData
});

async function onData(status, intention, value) {
  console.log(status);
}

There is a lemonade in the fridge.
Add the following code at the and of fridge.js

fridge.js intentionStorage.createIntention({
  title: {
    en: 'Has lemonade',
  },
  input: 'ThankYou',
  output: 'Lemonade',
  onData: onData
});

async function onData(status, intention, value) {
  console.log(status, value);
}

Launch two files in the different consoles.

node fridge.js
node victor.js

When counter intention for Victor was found, the accepting process will starts
All devices will receive message with status accept
All storage works independently, so every device received two messages accept. One from Victor and on from Fridge
If you throw an exception at the moment of acceptance, the accepting process will ber rejected, and second device will receive message with error status.

Callback onData has three parameters. Status, Intention, Value.
Status - is a message status. The message can be sent with any status, but there is system statuses
    accept - The message is ask for accept from another device
    data - The another device sends data
    close - The device has closed data channel
    error - The device reports an error on its side
    completed - The device reports the success of the task

Intention - Counter intention from the device that sent the data
Value - This field stores device data if it is transferred

fridge.js

Let's modify the onData function for the Fridge so that Victor can find lemonade

async function onData (status, intention, value) {
  if (status == 'accepting') {
  //You can send the data to another device with intention.send
  intention.send('data', this, {
    description: 'I am a fridge, I live in kitchen',
    location: 'Second shelf, left corner',
    volume: '1 liter'
  });
}

Now all device with intention "Lemonade - ThankYou", will receive information about lemonade from fridge
Launch the code

node freezer.js
node victor.js

Now you can see the data about lemonade in the Victors`s console
Now when Victor creates intention to get lemonade, he knows where to find it.

History
Terminal