Basics

Intro

In this section we will show you how Fluence JS can be used to create a hello world application with Fluence stack.

Aqua code

Let's start with the aqua code first:
1
import Peer from "@fluencelabs/aqua-lib/builtin.aqua" -- (1)
2
3
service HelloWorld("hello-world"): -- (2)
4
hello(str: string)
5
getFortune() -> string
6
7
func sayHello(): -- (3)
8
HelloWorld.hello("Hello, world!")
9
10
func tellFortune() -> string: -- (4)
11
res <- HelloWorld.getFortune()
12
<- res
13
14
func getRelayTime() -> u64: -- (5)
15
on HOST_PEER_ID:
16
ts <- Peer.timestamp_ms()
17
<- ts
Copied!
We need to import definitions to call standard Peer operations (1)
This file has three definitions.
(2) is a service named HelloWorld. A Service interfaces functions executable on a peer. We will register a handler for this interface in our typescript application.
(3) and (4) are functions sayHello and tellFortune correspondingly. These functions very simple. The only thing the first one does is calling the hello method of HelloWorld service located on the current peer. Similarly tellFortune calls the getFortune method from the same service and returns the value to the caller. We will show you how to call these function from the typescript application.
Finally we have a function (5) which demonstrate how to work with the network. It asks the current time from the relay peer and return back the our peer.

Installing dependencies

Initialize an empty npm package:
1
npm init
Copied!
We will need these two packages for the application runtime
1
npm install @fluencelabs/fluence @fluencelabs/fluence-network-environment
Copied!
The first one is the SDK itself and the second is a maintained list of Fluence networks and nodes to connect to.
Aqua compiler cli has to be installed, but is not needed at runtime.
1
npm install --save-dev @fluencelabs/aqua
Copied!
Aqua comes with the standard library which can accessed from "@fluencelabs/aqua-lib" package. All the aqua packages are only needed at compiler time, so we install it as a development dependency
1
npm install --save-dev @fluencelabs/aqua-lib
Copied!
Also we might want to have aqua source files automatically recompiled on every save. We will take advantage of chokidar for that:
1
npm install --save-dev @fluencelabs/chokidar-cli
Copied!
And last, but no least we will need TypeScript
1
npm install --save-dev typescript
2
npx tsc --init
Copied!

Setting up aqua compiler

Let's put aqua described earlier into aqua/hello-world.aqua file. You probably want to keep the generated TypeScript in the same directory with other typescript files, usually src. Let's create the src/_aqua directory for that.
The overall project structure looks like this:
1
┣ aqua
2
┃ ┗ hello-world.aqua
3
┣ src
4
┃ ┣ _aqua
5
┃ ┃ ┗ hello-world.ts
6
┃ ┗ index.ts
7
┣ package-lock.json
8
┣ package.json
9
┗ tsconfig.json
Copied!
The Aqua compiler can be run with npm:
1
npx aqua -i ./aqua/ -o ./src/_aqua
Copied!
We recommend to store this logic inside a script in packages.json file:
1
{
2
...
3
"scripts": {
4
...
5
"compile-aqua": "aqua -i ./aqua/ -o ./src/_aqua", // (1)
6
"watch-aqua": "chokidar \"**/*.aqua\" -c \"npm run compile-aqua\"" // (2)
7
},
8
...
9
}
Copied!
compile-aqua (1) runs the compilation once, producing src/_aqua/hello-world.ts in our case watch-aqua (2) starts watching for any changes in .aqua files recompiling them on the fly

Using the compiled code in typescript application

Using the code generated by the compiler is as easy as calling a function. The compiler generates all the boilerplate needed to send a particle into the network and wraps it into a single call. It also generate a function for service callback registration. Note that all the type information and therefore type checking and code completion facilities are there!
Let's see how use generated code in our application. index.ts:
1
import { Fluence } from "@fluencelabs/fluence";
2
import { krasnodar } from "@fluencelabs/fluence-network-environment"; // (1)
3
import {
4
registerHelloWorld,
5
sayHello,
6
getRelayTime,
7
tellFortune,
8
} from "./_aqua/hello-world"; // (2)
9
10
async function main() {
11
await Fluence.start({ connectTo: krasnodar[0] }); // (3)
12
13
// (4)
14
registerHelloWorld({
15
hello: (str) => {
16
console.log(str);
17
},
18
getFortune: async () => {
19
await new Promise((resolve) => {
20
setTimeout(resolve, 1000);
21
});
22
return "Wealth awaits you very soon.";
23
},
24
});
25
26
await sayHello(); // (4)
27
28
console.log(await tellFortune()); // (6)
29
30
const relayTime = await getRelayTime();
31
32
console.log("The relay time is: ", new Date(relayTime).toLocaleString());
33
34
await Fluence.stop(); // (7)
35
}
36
37
main();
Copied!
(1) Import list of possible relay nodes (network environment)
(2) Aqua compiler provides functions which can be directly imported like any normal typescript function.
(3) A Fluence peer has to be started before running any application in Fluence Network. For the vast majority of use cases you should use Fluence facade to start and stop the peer. The start method accepts a parameters object which. The most common parameter is the address of the relay node the peer should connect to. In this example we are using the first node of the krasnodar network. If you do not specify the connectTo options will only be able to execute air on the local machine only. Please keep in mind that the init function is asynchronous.
For every exported service XXX definition in aqua code, the compiler provides a registerXXX counterpart. These functions provide a type-safe way of registering callback handlers for the services. The callbacks are executed when the appropriate service is called in aqua on the current peer. The handlers take form of the object where keys are the name of functions and the values are async functions used as the corresponding callbacks. For example in (4) we are registering handlers for HelloWorld service functions which outputs it's parameter to the console. Please note that the handlers can be implemented in both: synchronous and asynchronous way. The handler can be made asynchronous like any other function in javascript: either return a Promise or mark it with async keyword to take advantage of async-await pattern.
For every exported func XXX definition in aqua code, the compiler provides an async function which can be directly called from typescript. In (5, 6) we are calling exported aqua function with no arguments. Note that every function is asynchronous.
(7) You should call stop when the peer is no longer needed. As a rule of thumb all the peers should be uninitialized before destroying the application.
Let's try running the example:
1
node -r ts-node/register src/index.ts
Copied!
If everything has been done correctly yuo should see Hello, world! in the console.
The next section will cover in-depth and advanced usage of Fluence JS
The code from this section is available in on github
Last modified 4d ago