From e75a597baed4b013547e0001e5e8592ceff1cf8c Mon Sep 17 00:00:00 2001 From: boneyard93501 Date: Fri, 10 Sep 2021 07:58:16 +0000 Subject: [PATCH] GitBook: [2.0.0] 3 pages modified --- SUMMARY.md | 2 +- js-sdk/4_run_in_browser.md | 2 - js-sdk/5_run_in_node.md | 124 +------------------------------------ 3 files changed, 2 insertions(+), 126 deletions(-) delete mode 100644 js-sdk/4_run_in_browser.md diff --git a/SUMMARY.md b/SUMMARY.md index 38d2527..eda81f2 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -19,7 +19,7 @@ * [Concepts](js-sdk/1_concepts.md) * [Basics](js-sdk/2_basics.md) * [In-depth](js-sdk/3_in_depth.md) - * [Running app in nodejs](js-sdk/4_run_in_browser.md) + * [Running app in nodejs](js-sdk/5_run_in_node.md) * [Running app in browser](js-sdk/4_run_in_browser-1.md) * [Api reference](js-sdk/6-reference.md) * [Changelog](js-sdk/changelog.md) diff --git a/js-sdk/4_run_in_browser.md b/js-sdk/4_run_in_browser.md deleted file mode 100644 index 1367383..0000000 --- a/js-sdk/4_run_in_browser.md +++ /dev/null @@ -1,2 +0,0 @@ -# Running app in nodejs - diff --git a/js-sdk/5_run_in_node.md b/js-sdk/5_run_in_node.md index fbfbd54..1367383 100644 --- a/js-sdk/5_run_in_node.md +++ b/js-sdk/5_run_in_node.md @@ -1,124 +1,2 @@ -# Intro +# Running app in nodejs -JS SDK makes it easy to run applications in NodeJS environment. You can take full advantage of the javascript ecosystem and at the save time expose service to the Fluence Network. That makes is an excellent choice for quick prototyping of applications for Fluence Stack. - -# Calc app example - -Lets implement a very simple app which simulates a desk calculator. The calculator has internal memory and implements the following set of operations: - -- Add a number -- Subtract a number -- Multiply by a number -- Divide by a number -- Get the current memory state -- Reset the memory state to 0.0 - -First, let's write the service definition in aqua: - -``` --- service definition -service Calc("calc"): - add(n: f32) - subtract(n: f32) - multiply(n: f32) - divide(n: f32) - reset() - getResult() -> f32 -``` - -Now write the implementation for this service in typescript: - -```typescript -import { FluencePeer } from "@fluencelabs/fluence"; -import { krasnodar } from "@fluencelabs/fluence-network-environment"; -import { registerCalc, CalcDef, demoCalculation } from "./_aqua/calc"; - -class Calc implements CalcDef { - private _state: number = 0; - - add(n: number) { - this._state += n; - } - - subtract(n: number) { - this._state -= n; - } - - multiply(n: number) { - this._state *= n; - } - - divide(n: number) { - this._state /= n; - } - - reset() { - this._state = 0; - } - - getResult() { - return this._state; - } -} - -const keypress = async () => { - process.stdin.setRawMode(true); - return new Promise((resolve) => - process.stdin.once("data", () => { - process.stdin.setRawMode(false); - resolve(); - }) - ); -}; - -async function main() { - await FluencePeer.default.init({ - connectTo: krasnodar[0], - }); - - registerCalc(new Calc()); - - console.log("application started"); - console.log("peer id is: ", FluencePeer.default.connectionInfo.selfPeerId); - console.log( - "relay is: ", - FluencePeer.default.connectionInfo.connectedRelays[0] - ); - console.log("press any key to continue"); - await keypress(); - - await FluencePeer.default.uninit(); -} - -main(); -``` - -As you can see all the service logic has been implemented in typescript. You have full power of npm at your disposal. - -Now try running the application: - -```bash -> node -r ts-node/register src/index.ts - -application started -peer id is: 12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3 -relay is: 12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e -press any key to continue - -``` - -And the service can be called from aqua. For example: - -```bash -const peer ?= "12D3KooWLBkw4Tz8bRoSriy5WEpHyWfU11jEK3b5yCa7FBRDRWH3" -const relay ?= "12D3KooWSD5PToNiLQwKDXsu8JSysCwUt8BVUJEqCHcDe7P5h45e" - -func demoCalculation() -> f32: - on peer via relay - Calc.add(10) - Calc.multiply(5) - Calc.subtract(8) - Calc.divide(6) - res <- Calc.getResult() - <- res -```