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.
npm init
npm i intention-storage
In Intention Network each devices has intention storage. Let`s create intention storage for fridge
fridge.js
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
There is a lemonade in the fridge.
Add the following code at the and of fridge.js
fridge.js
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
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.