mirror of
https://github.com/fluencelabs/gitbook-docs
synced 2025-04-25 07:52:14 +00:00
Fluence JS documentation fixes (#25)
This commit is contained in:
parent
836b911495
commit
bbe456ed96
@ -15,13 +15,13 @@
|
|||||||
* [Marine Rust SDK](knowledge_aquamarine/marine/marine-rs-sdk.md)
|
* [Marine Rust SDK](knowledge_aquamarine/marine/marine-rs-sdk.md)
|
||||||
* [Tools](knowledge_tools.md)
|
* [Tools](knowledge_tools.md)
|
||||||
* [Node](node.md)
|
* [Node](node.md)
|
||||||
* [JS SDK](js-sdk/README.md)
|
* [Fluence JS](js-sdk/README.md)
|
||||||
* [Concepts](js-sdk/1_concepts.md)
|
* [Concepts](js-sdk/1_concepts.md)
|
||||||
* [Basics](js-sdk/2_basics.md)
|
* [Basics](js-sdk/2_basics.md)
|
||||||
* [In-depth](js-sdk/3_in_depth.md)
|
* [In-depth](js-sdk/3_in_depth.md)
|
||||||
* [Running app in nodejs](js-sdk/5_run_in_node.md)
|
* [Running app in nodejs](js-sdk/5_run_in_node.md)
|
||||||
* [Running app in browser](js-sdk/4_run_in_browser-1.md)
|
* [Running app in browser](js-sdk/4_run_in_browser-1.md)
|
||||||
* [Api reference](js-sdk/6-reference.md)
|
* [API reference](js-sdk/6-reference.md)
|
||||||
* [Changelog](js-sdk/changelog.md)
|
* [Changelog](js-sdk/changelog.md)
|
||||||
* [Security](knowledge_security.md)
|
* [Security](knowledge_security.md)
|
||||||
* [Tutorials](tutorials_tutorials/README.md)
|
* [Tutorials](tutorials_tutorials/README.md)
|
||||||
|
@ -1,8 +1,30 @@
|
|||||||
# Concepts
|
# Concepts
|
||||||
|
|
||||||
## Basic concepts
|
## Creating applications with Aqua language
|
||||||
|
|
||||||
`@fluencelabs/fluence` package export the `FluencePeer` class. This class implements the Fluence protocol for javascript-based environments. It provides all the necessary features to communicate with Fluence network namely:
|
The official way to write applications for Fluence is using Aqua programming language. Aqua compiler emits TypeScript or JavaScript which in turn can be called from a js-based environment. The compiler outputs code for the following entities:
|
||||||
|
|
||||||
|
1. Exported `func` declarations are turned into callable async functions
|
||||||
|
2. Exported `service` declarations are turned into functions which register callback handler in a typed manner
|
||||||
|
|
||||||
|
To learn more about Aqua see [aqua book](https://doc.fluence.dev/aqua-book/)
|
||||||
|
|
||||||
|
The building block of the application are:
|
||||||
|
|
||||||
|
- Aqua code for peer-to-peer communication
|
||||||
|
- Compiler cli package for aqua to \(java\)typescript compilation
|
||||||
|
- Initialization of the `FluencePeer`
|
||||||
|
- Application specific code \(java\)typescript in the framework of your choice
|
||||||
|
|
||||||
|
In the next section we see it in action
|
||||||
|
|
||||||
|
## Facade API
|
||||||
|
|
||||||
|
The main entry point `@fluencelabs/fluence` is `Fluence` facade. It provides easy way to start and stop the Fluence Peer. The facade API is enough for the most of the uses cases.
|
||||||
|
|
||||||
|
## Fluence peer in JS
|
||||||
|
|
||||||
|
`@fluencelabs/fluence` package also exports the `FluencePeer` class. This class implements the Fluence protocol for javascript-based environments. It provides all the necessary features to communicate with Fluence network namely:
|
||||||
|
|
||||||
1. Connectivity with one or many Fluence Node which allows sending particles to and receiving from other Peers
|
1. Connectivity with one or many Fluence Node which allows sending particles to and receiving from other Peers
|
||||||
2. The Peer Id identifying the node in the network
|
2. The Peer Id identifying the node in the network
|
||||||
@ -12,29 +34,10 @@
|
|||||||
|
|
||||||
Even though the js-based implementation closely resembles [node](https://github.com/fluencelabs/gitbook-docs/js-sdk/node.md) there are some considerable differences to the latter.
|
Even though the js-based implementation closely resembles [node](https://github.com/fluencelabs/gitbook-docs/js-sdk/node.md) there are some considerable differences to the latter.
|
||||||
|
|
||||||
`FluencePeer` does not host services composed of wasm modules. Instead it allows to register service call handlers directly in javascript. The Aqua language compiler creates a typed helpers for that task. Using Aqua compiler is strontly advised when working with JS SDK.
|
`FluencePeer` does not host services composed of wasm modules. Instead it allows to register service call handlers directly in javascript. The Aqua language compiler creates a typed helpers for that task.
|
||||||
|
|
||||||
Due to the limitations of browser-based environment `FluencePeer` cannot be discovered by it's Peer Id on it's own. To overcome this `FluencePeer` must use an existing node which will act as a `relay`. When a peer is connected through a relay it is considered to be `client`. The `FluencePeer` routes all it's particle through it's relay thus taking advantage of the peer discovery implemented on the node. A particle sent to the connected client must be routed through it's relay.
|
Due to the limitations of browser-based environment `FluencePeer` cannot be discovered by it's Peer Id on it's own. To overcome this `FluencePeer` must use an existing node which will act as a `relay`. When a peer is connected through a relay it is considered to be `client`. The `FluencePeer` routes all it's particle through it's relay thus taking advantage of the peer discovery implemented on the node. A particle sent to the connected client must be routed through it's relay.
|
||||||
|
|
||||||
The js-based peer does not implement the full set of builtin functions due the limitations described previously. E.g there is no builtins implementation for _kad_ or _srv_ services. However _op_ service is fully implemented. For the full descriptions of implemented builtins refer to [Api reference](https://github.com/fluencelabs/gitbook-docs/js-sdk/js-sdk/6_reference/modules.md)
|
The js-based peer does not implement the full set of builtin functions due the limitations described previously. E.g there is no built-ins implementation for _kad_ or _srv_ services. However _op_ service is fully implemented. For the full descriptions of implemented built-ins refer to [Api reference](https://github.com/fluencelabs/gitbook-docs/js-sdk/js-sdk/6_reference/modules.md)
|
||||||
|
|
||||||
In contrast with the node implementation `FluencePeer` can initiate new particles execution. Aqua compiler generates executable functions from `func` definitions in aqua code.
|
In contrast with the node implementation `FluencePeer` can initiate new particles execution. Aqua compiler generates executable functions from `func` definitions in aqua code.
|
||||||
|
|
||||||
## Creating applications with Aqua language
|
|
||||||
|
|
||||||
The official way to write applications for Fluence is using Aqua programming language. Aqua compiler emits TypeScript or JavaScript which in turn can be called from a js-based environemt. The compiler outputs code for the following entities:
|
|
||||||
|
|
||||||
1. Exported `func` declarations are turned into callable async functiokns
|
|
||||||
2. Exported `service` declarations are turned into functions which register callback handler in a typed manner
|
|
||||||
|
|
||||||
To learn more about Aqua see [aqua book](https://doc.fluence.dev/aqua-book/)
|
|
||||||
|
|
||||||
The building block of the application are:
|
|
||||||
|
|
||||||
* Aqua code for peer-to-peer communication
|
|
||||||
* Compiler cli package for aqua to \(java\)typescript compilation
|
|
||||||
* Initialization of the `FluencePeer`
|
|
||||||
* Application specific code \(java\)typescript in the framework of your choice
|
|
||||||
|
|
||||||
In the next section we see it in action
|
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Intro
|
## Intro
|
||||||
|
|
||||||
In this section we will show you how JS SDK can be used to create a hello world application with Fluence stack.
|
In this section we will show you how Fluence JS can be used to create a hello world application with Fluence stack.
|
||||||
|
|
||||||
## Aqua code
|
## Aqua code
|
||||||
|
|
||||||
@ -27,11 +27,11 @@ The first one is a service named `HelloWorld`. A Service interfaces functions ex
|
|||||||
|
|
||||||
The second definition is the function `sayHello`. The only thing the function is doing is calling the `hello` method of `HelloWorld` service located on the current peer. We will show you how to call this function from the typescript application.
|
The second definition is the function `sayHello`. The only thing the function is doing is calling the `hello` method of `HelloWorld` service located on the current peer. We will show you how to call this function from the typescript application.
|
||||||
|
|
||||||
Finally we have a functions wich demomnstrate how to work with the network. It asks the current time from the relay peer and return back the our peer.
|
Finally we have a functions 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
|
## Installing dependencies
|
||||||
|
|
||||||
Initialze an empty npm package:
|
Initialize an empty npm package:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm init
|
npm init
|
||||||
@ -110,7 +110,7 @@ We recommend to store this logic inside a script in `packages.json` file:
|
|||||||
|
|
||||||
`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
|
`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 our application
|
## 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!
|
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!
|
||||||
|
|
||||||
@ -147,17 +147,17 @@ async function main() {
|
|||||||
main();
|
main();
|
||||||
```
|
```
|
||||||
|
|
||||||
\(1\) Import list of possible relay nodes \(network enironment\)
|
\(1\) Import list of possible relay nodes \(network environment\)
|
||||||
|
|
||||||
\(2\) Aqua compiler provides functions which can be directly imported like any normal typescript function.
|
\(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 asyncrhounous
|
\(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 funtions 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 \(3\) we are registering handler for `HelloWorld` service which outputs it's parameter to the console
|
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 \(3\) we are registering handler for `HelloWorld` service which outputs it's parameter to the console
|
||||||
|
|
||||||
For every exported `func XXX` definition in aqua code, the compiler provides an async function which can be directly called from typescripyt. In \(4, 5\) we are calling exported aqua function with no arguments. Note that every function is asyncrhonous.
|
For every exported `func XXX` definition in aqua code, the compiler provides an async function which can be directly called from typescript. In \(4, 5\) we are calling exported aqua function with no arguments. Note that every function is asynchronous.
|
||||||
|
|
||||||
\(6\) You should call `stop` when the peer is no longer needed. As a rule of thumb all the peers should be uninitilized before destroying the application.
|
\(6\) 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:
|
Let's try running the example:
|
||||||
|
|
||||||
@ -167,7 +167,6 @@ node -r ts-node/register src/index.ts
|
|||||||
|
|
||||||
If everything has been done correctly yuo should see `Hello, world!` in the console.
|
If everything has been done correctly yuo should see `Hello, world!` in the console.
|
||||||
|
|
||||||
The next secion will cover in-depth and advanced usage JS SDK
|
The next section will cover in-depth and advanced usage of Fluence JS
|
||||||
|
|
||||||
The code from this section is available in on \(github\)\[[https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/hello-world](https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/hello-world)\]
|
|
||||||
|
|
||||||
|
The code from this section is available in on [github](https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/hello-world)
|
||||||
|
@ -2,25 +2,25 @@
|
|||||||
|
|
||||||
## Intro
|
## Intro
|
||||||
|
|
||||||
In this section we will cover the JS SDK in-depth.
|
In this section we will cover the Fluence JS in-depth.
|
||||||
|
|
||||||
## Fluence
|
## Fluence
|
||||||
|
|
||||||
`@fluencelabs/fluence` exports a facade `Fluence` which provides all the needed functionality for the most uses cases. It defined 4 functions:
|
`@fluencelabs/fluence` exports a facade `Fluence` which provides all the needed functionality for the most uses cases. It defined 4 functions:
|
||||||
|
|
||||||
* `start`: Start the default peer.
|
- `start`: Start the default peer.
|
||||||
* `stop`: Stops the default peer
|
- `stop`: Stops the default peer
|
||||||
* `getStatus`: Gets the status of the default peer. This includes connection
|
- `getStatus`: Gets the status of the default peer. This includes connection
|
||||||
* `getPeer`: Gets the default Fluence Peer instance \(see below\)
|
- `getPeer`: Gets the default Fluence Peer instance \(see below\)
|
||||||
|
|
||||||
Under the hood `Fluence` facade calls the corresponding method on the default instance of FluencePeer. This instance is passed to the Aqua-compiler generated functions by default.
|
Under the hood `Fluence` facade calls the corresponding method on the default instance of FluencePeer. This instance is passed to the Aqua-compiler generated functions by default.
|
||||||
|
|
||||||
## FluencePeer class
|
## FluencePeer class
|
||||||
|
|
||||||
The second export `@fluencelabs/fluence` package is `FluencePeer` class. It is useful in scenarios when the application need to run everal different peer at once. The overall workflow with the `FluencePeer` is the following:
|
The second export `@fluencelabs/fluence` package is `FluencePeer` class. It is useful in scenarios when the application need to run several different peer at once. The overall workflow with the `FluencePeer` is the following:
|
||||||
|
|
||||||
1. Create an instance of the peer
|
1. Create an instance of the peer
|
||||||
2. Startign the peer
|
2. Starting the peer
|
||||||
3. Using the peer in the application
|
3. Using the peer in the application
|
||||||
4. Stopping the peer
|
4. Stopping the peer
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ To create a new peer simple instantiate the `FluencePeer` class:
|
|||||||
const peer = new FluencePeer();
|
const peer = new FluencePeer();
|
||||||
```
|
```
|
||||||
|
|
||||||
The constructor simply creates a new object and does not initialize any workflow. The `start` function starts the Aqua VM, initializes the default call service handlers and \(optionally\) connect to the Fluence network. The function takes an optional object specifying additonal peer configuration. On option you will be using a lot is `connectTo`. It tells the peer to connect to a relay. For example:
|
The constructor simply creates a new object and does not initialize any workflow. The `start` function starts the Aqua VM, initializes the default call service handlers and \(optionally\) connect to the Fluence network. The function takes an optional object specifying additional peer configuration. On option you will be using a lot is `connectTo`. It tells the peer to connect to a relay. For example:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
await peer.star({
|
await peer.star({
|
||||||
@ -38,17 +38,17 @@ await peer.star({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
connects the first node of the Kranodar network. You can find the officially maintained list networks in the `@fluencelabs/fluence-network-environment` package. The full list of supported options is described in the [API reference](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/js-sdk/6_reference/modules.md)
|
connects the first node of the Krasnodar network. You can find the officially maintained list networks in the `@fluencelabs/fluence-network-environment` package. The full list of supported options is described in the [API reference](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/js-sdk/6_reference/modules.md)
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
await peer.stop();
|
await peer.stop();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Using multiple peers in one applicaton
|
## Using multiple peers in one application
|
||||||
|
|
||||||
The peer by itself does not do any useful work. You should take advantage of functions generated by the Aqua compiler.
|
The peer by itself does not do any useful work. You should take advantage of functions generated by the Aqua compiler.
|
||||||
|
|
||||||
If your application needs several peers, you should create a separate `FluncePeer` instance for each of them. The generated functions accept the peer as the first argument. For example:
|
If your application needs several peers, you should create a separate `FluencePeer` instance for each of them. The generated functions accept the peer as the first argument. For example:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { FluencePeer } from "@fluencelabs/fluence";
|
import { FluencePeer } from "@fluencelabs/fluence";
|
||||||
@ -71,7 +71,7 @@ async function main() {
|
|||||||
|
|
||||||
// ... more application logic
|
// ... more application logic
|
||||||
|
|
||||||
// Pass the peer as the first agument
|
// Pass the peer as the first argument
|
||||||
// ||
|
// ||
|
||||||
// \/
|
// \/
|
||||||
registerSomeService(peer1, {
|
registerSomeService(peer1, {
|
||||||
@ -80,7 +80,7 @@ async function main() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Pass the peer as the first agument
|
// Pass the peer as the first argument
|
||||||
// ||
|
// ||
|
||||||
// \/
|
// \/
|
||||||
registerSomeService(peer2, {
|
registerSomeService(peer2, {
|
||||||
@ -89,7 +89,7 @@ async function main() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Pass the peer as the first agument
|
// Pass the peer as the first argument
|
||||||
// ||
|
// ||
|
||||||
// \/
|
// \/
|
||||||
await someCallableFunction(peer1, arg1, arg2, arg3);
|
await someCallableFunction(peer1, arg1, arg2, arg3);
|
||||||
@ -105,17 +105,17 @@ async function main() {
|
|||||||
It is possible to combine usage of the default peer with another one. Pay close attention to which peer you are calling the functions against.
|
It is possible to combine usage of the default peer with another one. Pay close attention to which peer you are calling the functions against.
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Registering handler for the default peer
|
// Registering handler for the default peerS
|
||||||
registerSomeService({
|
registerSomeService({
|
||||||
handler: async (str) => {
|
handler: async (str) => {
|
||||||
console.log("Called agains the default peer: " str);
|
console.log("Called against the default peer: " str);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Pay close attention to this
|
// Pay close attention to this
|
||||||
// ||
|
// ||
|
||||||
// \/
|
// \/
|
||||||
registerSomeService(someOthePeer, {
|
registerSomeService(someOtherPeer, {
|
||||||
handler: async (str) => {
|
handler: async (str) => {
|
||||||
console.log("Called against the peer named someOtherPeer: " str);
|
console.log("Called against the peer named someOtherPeer: " str);
|
||||||
},
|
},
|
||||||
@ -124,9 +124,9 @@ It is possible to combine usage of the default peer with another one. Pay close
|
|||||||
|
|
||||||
## Understanding the Aqua compiler output
|
## Understanding the Aqua compiler output
|
||||||
|
|
||||||
Aqua compiler emits TypeScript or JavaScript which in turn can be called from a js-based environemt. The compiler outputs code for the following entities:
|
Aqua compiler emits TypeScript or JavaScript which in turn can be called from a js-based environment. The compiler outputs code for the following entities:
|
||||||
|
|
||||||
1. Exported `func` declarations are turned into callable async functioks
|
1. Exported `func` declarations are turned into callable async functions
|
||||||
2. Exported `service` declarations are turned into functions which register callback handler in a typed manner
|
2. Exported `service` declarations are turned into functions which register callback handler in a typed manner
|
||||||
3. For every exported `service` the compiler generated it's interface under the name `{serviceName}Def`
|
3. For every exported `service` the compiler generated it's interface under the name `{serviceName}Def`
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ Aqua compiler emits TypeScript or JavaScript which in turn can be called from a
|
|||||||
|
|
||||||
For every exported function definition in aqua the compiler generated two overloads. One accepting the `FluencePeer` instance as the first argument, and one without it. Otherwise arguments are the same and correspond to the arguments of aqua functions. The last argument is always an optional config object with the following properties:
|
For every exported function definition in aqua the compiler generated two overloads. One accepting the `FluencePeer` instance as the first argument, and one without it. Otherwise arguments are the same and correspond to the arguments of aqua functions. The last argument is always an optional config object with the following properties:
|
||||||
|
|
||||||
* `ttl`: Optional parameter which specify TTL \(time to live\) of particle with execution logic for the function
|
- `ttl`: Optional parameter which specify TTL \(time to live\) of particle with execution logic for the function
|
||||||
|
|
||||||
The return type is always a promise of the aqua function return type. If the function does not return anything, the return type will be `Promise<void>`.
|
The return type is always a promise of the aqua function return type. If the function does not return anything, the return type will be `Promise<void>`.
|
||||||
|
|
||||||
@ -183,9 +183,9 @@ export function registerServiceName(
|
|||||||
): void;
|
): void;
|
||||||
```
|
```
|
||||||
|
|
||||||
* `peer` - the Fluence Peer instance where the handler should be registered. The peer can be ommited. In that case the default Fluence Peer will be used instead
|
- `peer` - the Fluence Peer instance where the handler should be registered. The peer can be omitted. In that case the default Fluence Peer will be used instead
|
||||||
* `serviceId` - the name of the service id. If the service was defined with the default service id in aqua code, this argument can be ommited.
|
- `serviceId` - the name of the service id. If the service was defined with the default service id in aqua code, this argument can be omitted.
|
||||||
* `service` - the handler for the service.
|
- `service` - the handler for the service.
|
||||||
|
|
||||||
Depending on whether or not the services was defined with the default id the number of overloads will be different. In the case it **is defined**, there would be four overloads:
|
Depending on whether or not the services was defined with the default id the number of overloads will be different. In the case it **is defined**, there would be four overloads:
|
||||||
|
|
||||||
@ -270,17 +270,17 @@ export interface CalcDef {
|
|||||||
|
|
||||||
`CallParams` will be described later in the section
|
`CallParams` will be described later in the section
|
||||||
|
|
||||||
### Type convertion
|
### Type conversion
|
||||||
|
|
||||||
Basic types convertion is pretty much straightforward:
|
Basic types conversion is pretty much straightforward:
|
||||||
|
|
||||||
* `string` is converted to `string` in typescript
|
- `string` is converted to `string` in typescript
|
||||||
* `bool` is converted to `boolean` in typescript
|
- `bool` is converted to `boolean` in typescript
|
||||||
* All number types \(`u8`, `u16`, `u32`, `u64`, `s8`, `s16`, `s32`, `s64`, `f32`, `f64`\) are converted to `number` in typescript
|
- All number types \(`u8`, `u16`, `u32`, `u64`, `s8`, `s16`, `s32`, `s64`, `f32`, `f64`\) are converted to `number` in typescript
|
||||||
|
|
||||||
Arrow types translate to functions in typescript which have their arguments translated to typescript types. In addition to arguments defined in aqua, typescript counterparts have an additional argument for call params. For the majority of use cases this parameter is not needed and can be ommited.
|
Arrow types translate to functions in typescript which have their arguments translated to typescript types. In addition to arguments defined in aqua, typescript counterparts have an additional argument for call params. For the majority of use cases this parameter is not needed and can be omitted.
|
||||||
|
|
||||||
The type convertion works the same way for `service` and `func` definitions. For example a `func` with a callback might look like this:
|
The type conversion works the same way for `service` and `func` definitions. For example a `func` with a callback might look like this:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
func callMeBack(callback: string, i32 -> ()):
|
func callMeBack(callback: string, i32 -> ()):
|
||||||
@ -312,4 +312,3 @@ Tetraplets have the form of:
|
|||||||
To learn more about tetraplets and application security see [Security](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/knowledge_security.md)
|
To learn more about tetraplets and application security see [Security](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/knowledge_security.md)
|
||||||
|
|
||||||
To see full specification of `CallParms` type see [Api reference](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/js-sdk/6_reference/modules.md)
|
To see full specification of `CallParms` type see [Api reference](https://github.com/fluencelabs/gitbook-docs/tree/77344eb147c2ce17fe1c0f37013082fc85c1ffa3/js-sdk/js-sdk/6_reference/modules.md)
|
||||||
|
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
# Running app in browser
|
# Running app in browser
|
||||||
|
|
||||||
You can use the JS SDK with any framework \(or even without it\). The "flunce" part of the application is a collection of pure typesctipt\javascript functions which can be called withing any framework of your choosing.
|
You can use the Fluence JS with any framework \(or even without it\). The "fluence" part of the application is a collection of pure typescript\javascript functions which can be called withing any framework of your choosing.
|
||||||
|
|
||||||
See the browser-example which demonstrate integrating Fluence with React: \(github\)\[[https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/browser-example](https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/browser-example)\]
|
See the browser-example which demonstrate integrating Fluence with React: [github](https://github.com/fluencelabs/examples/tree/main/js-sdk-examples/browser-example)
|
||||||
|
|
||||||
Also take a look at FluentPad. It is an example application written in React: [https://github.com/fluencelabs/fluent-pad](https://github.com/fluencelabs/fluent-pad)
|
Also take a look at FluentPad. It is an example application written in React: [https://github.com/fluencelabs/fluent-pad](https://github.com/fluencelabs/fluent-pad)
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
## Intro
|
## Intro
|
||||||
|
|
||||||
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.
|
It is easy to use Fluence JS in NodeJS applications. 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
|
## Calc app example
|
||||||
|
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
# Api reference
|
# API reference
|
||||||
|
|
||||||
|
API reference is available at https://fluence.network/fluence-js/
|
||||||
|
@ -1,128 +0,0 @@
|
|||||||
# Class: FluencePeer
|
|
||||||
|
|
||||||
This class implements the Fluence protocol for javascript-based environments.
|
|
||||||
It provides all the necessary features to communicate with Fluence network
|
|
||||||
|
|
||||||
## Table of contents
|
|
||||||
|
|
||||||
### Constructors
|
|
||||||
|
|
||||||
- [constructor](js-sdk/6_reference/classes/FluencePeer.md#constructor)
|
|
||||||
|
|
||||||
### Accessors
|
|
||||||
|
|
||||||
- [connectionInfo](js-sdk/6_reference/classes/FluencePeer.md#connectioninfo)
|
|
||||||
- [internals](js-sdk/6_reference/classes/FluencePeer.md#internals)
|
|
||||||
- [default](js-sdk/6_reference/classes/FluencePeer.md#default)
|
|
||||||
|
|
||||||
### Methods
|
|
||||||
|
|
||||||
- [init](js-sdk/6_reference/classes/FluencePeer.md#init)
|
|
||||||
- [uninit](js-sdk/6_reference/classes/FluencePeer.md#uninit)
|
|
||||||
|
|
||||||
## Constructors
|
|
||||||
|
|
||||||
### constructor
|
|
||||||
|
|
||||||
• **new FluencePeer**()
|
|
||||||
|
|
||||||
Creates a new Fluence Peer instance. Does not start the workflows.
|
|
||||||
In order to work with the Peer it has to be initialized with the `init` method
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:111](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L111)
|
|
||||||
|
|
||||||
## Accessors
|
|
||||||
|
|
||||||
### connectionInfo
|
|
||||||
|
|
||||||
• `get` **connectionInfo**(): `ConnectionInfo`
|
|
||||||
|
|
||||||
Get the information about Fluence Peer connections
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`ConnectionInfo`
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:116](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L116)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### internals
|
|
||||||
|
|
||||||
• `get` **internals**(): `Object`
|
|
||||||
|
|
||||||
Does not intended to be used manually. Subject to change
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`Object`
|
|
||||||
|
|
||||||
| Name | Type |
|
|
||||||
| :------ | :------ |
|
|
||||||
| `callServiceHandler` | `CallServiceHandler` |
|
|
||||||
| `initiateFlow` | (`request`: `RequestFlow`) => `Promise`<`void`\> |
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:190](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L190)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### default
|
|
||||||
|
|
||||||
• `Static` `get` **default**(): [`FluencePeer`](js-sdk/6_reference/classes/FluencePeer.md)
|
|
||||||
|
|
||||||
Get the default Fluence peer instance. The default peer is used automatically in all the functions generated
|
|
||||||
by the Aqua compiler if not specified otherwise.
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
[`FluencePeer`](js-sdk/6_reference/classes/FluencePeer.md)
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:181](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L181)
|
|
||||||
|
|
||||||
## Methods
|
|
||||||
|
|
||||||
### init
|
|
||||||
|
|
||||||
▸ **init**(`config?`): `Promise`<`void`\>
|
|
||||||
|
|
||||||
Initializes the peer: starts the Aqua VM, initializes the default call service handlers
|
|
||||||
and (optionally) connect to the Fluence network
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
|
|
||||||
| Name | Type | Description |
|
|
||||||
| :------ | :------ | :------ |
|
|
||||||
| `config?` | `PeerConfig` | object specifying peer configuration |
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`Promise`<`void`\>
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:130](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L130)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### uninit
|
|
||||||
|
|
||||||
▸ **uninit**(): `Promise`<`void`\>
|
|
||||||
|
|
||||||
Uninitializes the peer: stops all the underltying workflows, stops the Aqua VM
|
|
||||||
and disconnects from the Fluence network
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`Promise`<`void`\>
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:172](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L172)
|
|
@ -1,92 +0,0 @@
|
|||||||
# Interface: CallParams<ArgName\>
|
|
||||||
|
|
||||||
Additional information about a service call
|
|
||||||
|
|
||||||
## Type parameters
|
|
||||||
|
|
||||||
| Name | Type |
|
|
||||||
| :------ | :------ |
|
|
||||||
| `ArgName` | extends `string` \| ``null`` |
|
|
||||||
|
|
||||||
## Table of contents
|
|
||||||
|
|
||||||
### Properties
|
|
||||||
|
|
||||||
- [initPeerId](js-sdk/6_reference/interfaces/CallParams.md#initpeerid)
|
|
||||||
- [particleId](js-sdk/6_reference/interfaces/CallParams.md#particleid)
|
|
||||||
- [signature](js-sdk/6_reference/interfaces/CallParams.md#signature)
|
|
||||||
- [tetraplets](js-sdk/6_reference/interfaces/CallParams.md#tetraplets)
|
|
||||||
- [timeStamp](js-sdk/6_reference/interfaces/CallParams.md#timestamp)
|
|
||||||
- [ttl](js-sdk/6_reference/interfaces/CallParams.md#ttl)
|
|
||||||
|
|
||||||
## Properties
|
|
||||||
|
|
||||||
### initPeerId
|
|
||||||
|
|
||||||
• **initPeerId**: `string`
|
|
||||||
|
|
||||||
The peer id which created the particle
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:36](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L36)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### particleId
|
|
||||||
|
|
||||||
• **particleId**: `string`
|
|
||||||
|
|
||||||
The identifier of particle which triggered the call
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:31](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L31)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### signature
|
|
||||||
|
|
||||||
• **signature**: `string`
|
|
||||||
|
|
||||||
Particle's signature
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:51](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L51)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### tetraplets
|
|
||||||
|
|
||||||
• **tetraplets**: { [key in string]: SecurityTetraplet[]}
|
|
||||||
|
|
||||||
Security tetraplets
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:56](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L56)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### timeStamp
|
|
||||||
|
|
||||||
• **timeStamp**: `number`
|
|
||||||
|
|
||||||
Particle's timestamp when it was created
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:41](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L41)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### ttl
|
|
||||||
|
|
||||||
• **ttl**: `number`
|
|
||||||
|
|
||||||
Time to live in milliseconds. The time after the particle should be expired
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:46](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L46)
|
|
@ -1,134 +0,0 @@
|
|||||||
# @fluencelabs/fluence
|
|
||||||
|
|
||||||
## Table of contents
|
|
||||||
|
|
||||||
### Classes
|
|
||||||
|
|
||||||
- [FluencePeer](js-sdk/6_reference/classes/FluencePeer.md)
|
|
||||||
|
|
||||||
### Interfaces
|
|
||||||
|
|
||||||
- [CallParams](js-sdk/6_reference/interfaces/CallParams.md)
|
|
||||||
|
|
||||||
### Type aliases
|
|
||||||
|
|
||||||
- [AvmLoglevel](js-sdk/6_reference/modules.md#avmloglevel)
|
|
||||||
- [PeerIdB58](js-sdk/6_reference/modules.md#peeridb58)
|
|
||||||
|
|
||||||
### Functions
|
|
||||||
|
|
||||||
- [peerIdFromEd25519SK](js-sdk/6_reference/modules.md#peeridfromed25519sk)
|
|
||||||
- [peerIdToEd25519SK](js-sdk/6_reference/modules.md#peeridtoed25519sk)
|
|
||||||
- [randomPeerId](js-sdk/6_reference/modules.md#randompeerid)
|
|
||||||
- [setLogLevel](js-sdk/6_reference/modules.md#setloglevel)
|
|
||||||
|
|
||||||
## Type aliases
|
|
||||||
|
|
||||||
### AvmLoglevel
|
|
||||||
|
|
||||||
Ƭ **AvmLoglevel**: `LogLevel`
|
|
||||||
|
|
||||||
Enum representing the log level used in Aqua VM.
|
|
||||||
Possible values: 'info', 'trace', 'debug', 'info', 'warn', 'error', 'off';
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/FluencePeer.ts:35](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/FluencePeer.ts#L35)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### PeerIdB58
|
|
||||||
|
|
||||||
Ƭ **PeerIdB58**: `string`
|
|
||||||
|
|
||||||
Peer ID's id as a base58 string (multihash/CIDv0).
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/commonTypes.ts:22](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/commonTypes.ts#L22)
|
|
||||||
|
|
||||||
## Functions
|
|
||||||
|
|
||||||
### peerIdFromEd25519SK
|
|
||||||
|
|
||||||
▸ `Const` **peerIdFromEd25519SK**(`sk`): `Promise`<`PeerId`\>
|
|
||||||
|
|
||||||
Generates a new peer id from base64 string contatining the 32 byte Ed25519S secret key
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
|
|
||||||
| Name | Type |
|
|
||||||
| :------ | :------ |
|
|
||||||
| `sk` | `string` |
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`Promise`<`PeerId`\>
|
|
||||||
|
|
||||||
- Promise with the created Peer Id
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/peerIdUtils.ts:26](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/peerIdUtils.ts#L26)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### peerIdToEd25519SK
|
|
||||||
|
|
||||||
▸ `Const` **peerIdToEd25519SK**(`peerId`): `string`
|
|
||||||
|
|
||||||
Converts peer id into base64 string contatining the 32 byte Ed25519S secret key
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
|
|
||||||
| Name | Type |
|
|
||||||
| :------ | :------ |
|
|
||||||
| `peerId` | `PeerId` |
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`string`
|
|
||||||
|
|
||||||
- base64 of Ed25519S secret key
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/peerIdUtils.ts:45](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/peerIdUtils.ts#L45)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### randomPeerId
|
|
||||||
|
|
||||||
▸ `Const` **randomPeerId**(): `Promise`<`PeerId`\>
|
|
||||||
|
|
||||||
Generates a new peer id with random private key
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`Promise`<`PeerId`\>
|
|
||||||
|
|
||||||
- Promise with the created Peer Id
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[internal/peerIdUtils.ts:59](https://github.com/fluencelabs/fluence-js/blob/480d630/src/internal/peerIdUtils.ts#L59)
|
|
||||||
|
|
||||||
___
|
|
||||||
|
|
||||||
### setLogLevel
|
|
||||||
|
|
||||||
▸ `Const` **setLogLevel**(`level`): `void`
|
|
||||||
|
|
||||||
#### Parameters
|
|
||||||
|
|
||||||
| Name | Type |
|
|
||||||
| :------ | :------ |
|
|
||||||
| `level` | `LogLevelDesc` |
|
|
||||||
|
|
||||||
#### Returns
|
|
||||||
|
|
||||||
`void`
|
|
||||||
|
|
||||||
#### Defined in
|
|
||||||
|
|
||||||
[index.ts:23](https://github.com/fluencelabs/fluence-js/blob/480d630/src/index.ts#L23)
|
|
@ -1,6 +1,14 @@
|
|||||||
# JS SDK
|
# Fluence JS
|
||||||
|
|
||||||
JS SDK provides the implementation of the Fluence Protocol which can be hosted in js-based environment. Currently node.js 14+ and majority of the modern browsers are tested.
|
Fluence JS is an implementation of the Fluence protocol for JavaScript-based environments. It can connect browsers, Node.js applications, and so on to the Fluence p2p network.
|
||||||
|
|
||||||
The JS SDK is just a library wich can be used with any framework of your choice \(or even without frameworks\).
|
Similar to the [Rust Fluence Peer implementation](https://github.com/fluencelabs/fluence) it includes:
|
||||||
|
|
||||||
|
- Peer-to-peer communication layer (via [js-libp2p](https://github.com/libp2p/js-libp2p))
|
||||||
|
- [Aqua VM](https://github.com/fluencelabs/aquavm)
|
||||||
|
- Builtin services
|
||||||
|
|
||||||
|
Fluence JS can call services and functions on the Fluence network, and expose new APIs to the p2p network directly from TypeScript and JavaScript.
|
||||||
|
[Aqua language](https://github.com/fluencelabs/aqua) uses Fluence JS as a compilation target, and they are designed to [work in tandem](https://doc.fluence.dev/docs/js-sdk/3_in_depth#understanding-the-aqua-compiler-output).
|
||||||
|
|
||||||
|
Fluence JS can be used with any framework of your choice \(or even without frameworks\).
|
||||||
|
@ -1,4 +1,33 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
Similar to what we have in aqua book
|
Fluence JS versioning scheme is the following: `0.BREAKING.ENHANCING`
|
||||||
|
|
||||||
|
- `0` shows that Fluence JS does not meet its vision yet, so API can change quickly
|
||||||
|
- `BREAKING` part is incremented for each breaking API change
|
||||||
|
- `ENHANCING` part is incremented for every fix and update which is compatible on API level
|
||||||
|
|
||||||
|
### [0.13.0](https://github.com/fluencelabs/fluence-js/releases/tag/v0.13.0) – September 24, 2021
|
||||||
|
|
||||||
|
- `PeerStatus` is now exported from index file \([#80](https://github.com/fluencelabs/fluence-js/pull/80)\)
|
||||||
|
- `KeyPair`: method `fromBytes` is replaced with fromEd25519SK \([#81](https://github.com/fluencelabs/fluence-js/pull/81)\)
|
||||||
|
|
||||||
|
### [0.12.1](https://github.com/fluencelabs/fluence-js/releases/tag/v0.12.1) – September 14, 2021
|
||||||
|
|
||||||
|
- `KeyPair`: add fromBytes, toEd25519PrivateKey \([#78](https://github.com/fluencelabs/fluence-js/pull/78)\)
|
||||||
|
|
||||||
|
### [0.12.0](https://github.com/fluencelabs/fluence-js/releases/tag/v0.13.0) – September 10, 2021
|
||||||
|
|
||||||
|
- The API to work with the default Fluence Peer has been put under the facade `Fluence`. Method `init` was renamed to `start` and `uninit` renamed to `stop`. `connectionStatus` migrated to `getStatus`.
|
||||||
|
|
||||||
|
To migrate from 0.11.0 to 0.12.0
|
||||||
|
|
||||||
|
1. `import { Fluence } from "@fluencelabs/fluence"`; instead of `FluencePeer`
|
||||||
|
2. replace `Fluence.default` with just `Fluence`
|
||||||
|
3. replace `init` with `start` and `uninit` with `stop`
|
||||||
|
4. replace `connectionInfo()` with `getStatus()`
|
||||||
|
|
||||||
|
\([#72](https://github.com/fluencelabs/fluence-js/pull/72)\)
|
||||||
|
|
||||||
|
### [0.11.0](https://github.com/fluencelabs/fluence-js/releases/tag/v0.11.0) – September 08, 2021
|
||||||
|
|
||||||
|
- Update JS SDK api to the new version \([#61](https://github.com/fluencelabs/fluence-js/pull/61)\)
|
||||||
|
@ -72,7 +72,7 @@ Options:
|
|||||||
[number] [required] [default: 60000]
|
[number] [required] [default: 60000]
|
||||||
```
|
```
|
||||||
|
|
||||||
## Fluence JS SDK
|
## Fluence JS
|
||||||
|
|
||||||
The Fluence [JS SDK](https://github.com/fluencelabs/fluence-js) supports developers to build full-fledged applications for a variety of targets ranging from browsers to backend apps and greatly expands on the `fldist` capabilities.
|
The [Fluence JS](https://github.com/fluencelabs/fluence-js) supports developers to build full-fledged applications for a variety of targets ranging from browsers to backend apps and greatly expands on the `fldist` capabilities.
|
||||||
|
|
||||||
|
@ -9,10 +9,10 @@ Each Fluence peer is equipped with a set of "built-in" services that can be call
|
|||||||
3. _srv_ – management and information about services on a node
|
3. _srv_ – management and information about services on a node
|
||||||
4. _dist_ – distribution and inspection of modules and blueprints
|
4. _dist_ – distribution and inspection of modules and blueprints
|
||||||
5. _script_ – to manage recurring scripts
|
5. _script_ – to manage recurring scripts
|
||||||
6. _op_ – basic operations on data deprecated - namespace for deprecated API Below is the reference documentation for all the existing built-in services. Please refer to the JS SDK documentation to learn how to easily use them from the JS SDK
|
6. _op_ – basic operations on data deprecated - namespace for deprecated API Below is the reference documentation for all the existing built-in services. Please refer to the Fluence JS documentation to learn how to easily use them from the Fluence JS
|
||||||
7. _deprecated_ - namespace for deprecated API
|
7. _deprecated_ - namespace for deprecated API
|
||||||
|
|
||||||
Please note that the [`fldist`](knowledge_tools.md#fluence-proto-distributor-fldist) CLI tool, as well as the [JS SDK](knowledge_tools.md#fluence-js-sdk), provide access to node-based services.
|
Please note that the [`fldist`](knowledge_tools.md#fluence-proto-distributor-fldist) CLI tool, as well as the [Fluence JS](knowledge_tools.md#fluence-js-sdk), provide access to node-based services.
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
@ -39,9 +39,9 @@ Options:
|
|||||||
[number] [required] [default: 60000]
|
[number] [required] [default: 60000]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Fluence JS SDK
|
### Fluence JS
|
||||||
|
|
||||||
The Fluence [JS SDK](https://github.com/fluencelabs/fluence-js) supports developers to build full-fledged applications for a variety of targets ranging from browsers to backend apps and greatly expands on the `fldist` capabilities.
|
The [Fluence JS](https://github.com/fluencelabs/fluence-js) supports developers to build full-fledged applications for a variety of targets ranging from browsers to backend apps and greatly expands on the `fldist` capabilities.
|
||||||
|
|
||||||
### Marine Tools
|
### Marine Tools
|
||||||
|
|
||||||
|
@ -9,10 +9,10 @@ Each Fluence node is equipped with a set of "builtin" services that can be calle
|
|||||||
3. _srv_ – management and information about services on a node
|
3. _srv_ – management and information about services on a node
|
||||||
4. _dist_ – distribution and inspection of modules and blueprints
|
4. _dist_ – distribution and inspection of modules and blueprints
|
||||||
5. _script_ – to manage recurring scripts
|
5. _script_ – to manage recurring scripts
|
||||||
6. _op_ – basic operations on data deprecated - namespace for deprecated API Below is the reference documentation for all the existing built-in services. Please refer to the JS SDK documentation to learn how to easily use them from the JS SDK
|
6. _op_ – basic operations on data deprecated - namespace for deprecated API Below is the reference documentation for all the existing built-in services. Please refer to the Fluence JS documentation to learn how to easily use them from the Fluence JS
|
||||||
7. _deprecated_ - namespace for deprecated API
|
7. _deprecated_ - namespace for deprecated API
|
||||||
|
|
||||||
Please note that the [`fldist`](../knowledge_tools.md#fluence-proto-distributor-fldist) CLI tool, as well as the [JS SDK](../knowledge_tools.md#fluence-js-sdk), provide access to node-based services.
|
Please note that the [`fldist`](../knowledge_tools.md#fluence-proto-distributor-fldist) CLI tool, as well as the [Fluence JS](../knowledge_tools.md#fluence-js-sdk), provide access to node-based services.
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
@ -1,4 +0,0 @@
|
|||||||
# Building a Chat Appplication
|
|
||||||
|
|
||||||
Coming soon. If you really need this section, contact us through any of the social media channels or Github.
|
|
||||||
|
|
@ -1,4 +0,0 @@
|
|||||||
# Building a Collaborative Editor
|
|
||||||
|
|
||||||
Coming soon. If you really need this section, contact us through any of the social media channels or Github.
|
|
||||||
|
|
@ -1,241 +0,0 @@
|
|||||||
# Building a Frontend with JS-SDK
|
|
||||||
|
|
||||||
Fluence provides means to connect to the network from a javascript environment. It is currently tested to work in nodejs and modern browsers.
|
|
||||||
|
|
||||||
To create an application you will need two main building blocks: the JS SDK itself and the aqua compiler. Both of them are provided in a form npm packages. JS SDK wraps the air interpreter and provides a connection to the network. There is low-level api for executing air scripts and registering for service calls handlers. Aqua compiler allows to write code in aqua language and compile it into typescript code which can be directly used with the SDK.
|
|
||||||
|
|
||||||
Even though all the logic could be programmed by hand with raw air it is strongly recommended to use aqua.
|
|
||||||
|
|
||||||
### Basic usage
|
|
||||||
|
|
||||||
As previously said you can use Fluence with any frontend or nodejs framework. JS SDK could be as any other npm library. For the purpose of the demo we will init a bare-bones nodejs package and show you the steps needed install JS SDK and aqua compiler. Feel free to use the tool most suitable for the framework used in application, the installation process should be roughly the same
|
|
||||||
|
|
||||||
#### 1. Start with npm package
|
|
||||||
|
|
||||||
For the purpose of the demo we will use a very minimal npm package with typescript support:
|
|
||||||
|
|
||||||
```text
|
|
||||||
src
|
|
||||||
┗ index.ts (1)
|
|
||||||
package.json (2)
|
|
||||||
tsconfig.json
|
|
||||||
```
|
|
||||||
|
|
||||||
index.ts \(1\):
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
async function main() {
|
|
||||||
console.log("Hello, world!");
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
```
|
|
||||||
|
|
||||||
package.json \(2\):
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
{
|
|
||||||
"name": "demo",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"exec": "node -r ts-node/register src/index.ts"
|
|
||||||
},
|
|
||||||
"author": "",
|
|
||||||
"license": "ISC",
|
|
||||||
"devDependencies": {
|
|
||||||
"ts-node": "^9.1.1",
|
|
||||||
"typescript": "^4.2.4"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Let's test if it works:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ npm install
|
|
||||||
$ npm run exec
|
|
||||||
```
|
|
||||||
|
|
||||||
The following should be printed
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ npm run exec
|
|
||||||
|
|
||||||
> demo@1.0.0 exec C:\work\demo
|
|
||||||
> node -r ts-node/register src/index.ts
|
|
||||||
|
|
||||||
Hello, world!
|
|
||||||
$ C:\work\demo>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 2. Setting JS SDK and connecting to Fluence network
|
|
||||||
|
|
||||||
Install the dependencies, you will need these two packages.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install @fluencelabs/fluence @fluencelabs/fluence-network-environment
|
|
||||||
```
|
|
||||||
|
|
||||||
The first one is the SDK itself and the second is a maintained list of Fluence networks and nodes to connect to.
|
|
||||||
|
|
||||||
All of the communication with the Fluence network is done by using `FluenceClient`. You can create one with `createClient` function. The client encapsulates air interpreter and connects to the network through the relay. Currently any node in the network can be used a relay.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { createClient } from "@fluencelabs/fluence";
|
|
||||||
import { testNet } from "@fluencelabs/fluence-network-environment";
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const client = await createClient(testNet[1]);
|
|
||||||
console.log("Is client connected: ", client.isConnected);
|
|
||||||
|
|
||||||
await client.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
```
|
|
||||||
|
|
||||||
Let's try it out:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ npm run exec
|
|
||||||
|
|
||||||
> demo@1.0.0 exec C:\work\demo
|
|
||||||
> node -r ts-node/register src/index.ts
|
|
||||||
|
|
||||||
Is client connected: true
|
|
||||||
$
|
|
||||||
```
|
|
||||||
|
|
||||||
**Note**: typically you should have a single instance`FluenceClient` per application since it represents it's identity in the network. You are free to store the instance anywhere you like.
|
|
||||||
|
|
||||||
#### 3. Setting Up The Aqua Compiler
|
|
||||||
|
|
||||||
Aqua is the proffered language for the Fluence network. It can be used with javascript-based environments via npm package.
|
|
||||||
|
|
||||||
{% hint style="warning" %}
|
|
||||||
**The package requires java to be installed as it calls "java -jar ... "**
|
|
||||||
{% endhint %}
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install --save-dev @fluencelabs/aqua
|
|
||||||
```
|
|
||||||
|
|
||||||
We will also need the standard library for the language
|
|
||||||
|
|
||||||
```text
|
|
||||||
npm install --save-dev @fluencelabs/aqua-lib
|
|
||||||
```
|
|
||||||
|
|
||||||
Let's add our first aqua file:
|
|
||||||
|
|
||||||
```text
|
|
||||||
aqua (1)
|
|
||||||
┗ demo.aqua (2)
|
|
||||||
node_modules
|
|
||||||
src
|
|
||||||
┣ compiled (3)
|
|
||||||
┗ index.ts
|
|
||||||
package-lock.json
|
|
||||||
package.json
|
|
||||||
tsconfig.json
|
|
||||||
```
|
|
||||||
|
|
||||||
Add two directories, one for aqua sources \(1\) and another for the typescript output \(3\)
|
|
||||||
|
|
||||||
Create a new text file called `demo.aqua` \(2\):
|
|
||||||
|
|
||||||
```text
|
|
||||||
import "@fluencelabs/aqua-lib/builtin.aqua"
|
|
||||||
|
|
||||||
func demo(nodePeerId: PeerId) -> []string:
|
|
||||||
on nodePeerId:
|
|
||||||
res <- Peer.identify()
|
|
||||||
<- res.external_addresses
|
|
||||||
```
|
|
||||||
|
|
||||||
This script will gather the list of external addresses from some node in the network. For more information about the aqua language refer to the aqua documentation.
|
|
||||||
|
|
||||||
The aqua code can now be compiled by using the compiler CLI. We suggest adding a script to the package.json file like so:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
...
|
|
||||||
"scripts": {
|
|
||||||
"exec": "node -r ts-node/register src/index.ts",
|
|
||||||
"compile-aqua": "aqua -i ./aqua/ -o ./src/compiled"
|
|
||||||
},
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
Run the compiler:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm run compile-aqua
|
|
||||||
```
|
|
||||||
|
|
||||||
A typescript file should be generated like so:
|
|
||||||
|
|
||||||
```text
|
|
||||||
aqua
|
|
||||||
┗ demo.aqua
|
|
||||||
node_modules
|
|
||||||
src
|
|
||||||
┣ compiled
|
|
||||||
┃ ┗ demo.ts <--
|
|
||||||
┗ index.ts
|
|
||||||
package-lock.json
|
|
||||||
package.json
|
|
||||||
tsconfig.json
|
|
||||||
```
|
|
||||||
|
|
||||||
#### 4. Consuming The Compiled Code
|
|
||||||
|
|
||||||
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. Note that all the type information and therefore type checking and code completion facilities are there!
|
|
||||||
|
|
||||||
Let's do it!
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { createClient } from "@fluencelabs/fluence";
|
|
||||||
import { testNet } from "@fluencelabs/fluence-network-environment";
|
|
||||||
|
|
||||||
import { demo } from "./compiled/demo"; // import the generated file
|
|
||||||
|
|
||||||
async function main() {
|
|
||||||
const client = await createClient(testNet[1]);
|
|
||||||
console.log("Is client connected: ", client.isConnected);
|
|
||||||
|
|
||||||
const otherNode = testNet[2].peerId;
|
|
||||||
const addresses = await demo(client, otherNode); // call it like a normal function in typescript
|
|
||||||
console.log(`Node ${otherNode} is connected to: ${addresses}`);
|
|
||||||
|
|
||||||
await client.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
main();
|
|
||||||
```
|
|
||||||
|
|
||||||
if everything is fine you have similar result:
|
|
||||||
|
|
||||||
```text
|
|
||||||
$ npm run exec
|
|
||||||
|
|
||||||
> demo@1.0.0 exec C:\work\demo
|
|
||||||
> node -r ts-node/register src/index.ts
|
|
||||||
|
|
||||||
Is client connected: true
|
|
||||||
Node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er is connected to: /ip4/138.197.189.50/tcp/7001,/ip4/138.197.189.50/tcp/9001/ws
|
|
||||||
|
|
||||||
$
|
|
||||||
```
|
|
||||||
|
|
||||||
### Advanced usage
|
|
||||||
|
|
||||||
Fluence JS SDK gives options to register own handlers for aqua vm service calls
|
|
||||||
|
|
||||||
**TBD**
|
|
||||||
|
|
||||||
### References
|
|
||||||
|
|
||||||
- For the list of compiler options see: [https://github.com/fluencelabs/aqua](https://github.com/fluencelabs/aqua)
|
|
||||||
- Repository with additional examples: [**https://github.com/fluencelabs/aqua-playground**](https://github.com/fluencelabs/aqua-playground)\*\*\*\*
|
|
@ -1,4 +0,0 @@
|
|||||||
# Developing a Frontend Application with JS-SDK
|
|
||||||
|
|
||||||
Coming soon. If you really need this section, contact us through any of the social media channels or Github.
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user