JS/TS Peer for the Fluence p2p network
Go to file
2024-08-13 14:11:53 +03:00
.fluence feat(fluence-network-environment): Introduce fluence-network-environment package (#442) 2024-03-05 02:13:21 +07:00
.github fix(ci): ignore error on setup-fluence 2024-08-13 14:11:53 +03:00
packages fix(deps): use external fluence-network-environment 1.2.2 [fixes FLU-689] (#454) 2024-07-31 14:52:10 +03:00
resources feat: update copyright (#449) 2024-05-16 12:34:31 +03:00
.eslintrc.json fix: Update libp2p deps (#419) 2024-01-19 09:25:12 +00:00
.gitignore fix: JS-client bugs and tech debt [fixes DXJ-520] (#374) 2023-11-06 11:42:24 +01:00
.npmrc feat(js-client)!: Particle signatures [fixes DXJ-466] (#353) 2023-10-02 19:39:13 +07:00
.prettierignore chore: Use fluence-cli to setup network (#441) 2024-02-27 09:53:18 +02:00
.prettierrc feat(js-client)!: Adding strictes eslint and ts config to all packages [fixes DXJ-464] (#355) 2023-10-17 22:14:08 +07:00
ci.cjs feat(js-client)!: Segregation of responsibility between js-client packages [fixes DXJ-525] (#378) 2023-11-19 09:04:10 +07:00
CONTRIBUTING.md feat(js-client)!: Adding strictes eslint and ts config to all packages [fixes DXJ-464] (#355) 2023-10-17 22:14:08 +07:00
DEVELOPING.md feat(docs): README edited to represent the current JS Client API (#256) 2023-02-16 17:45:37 +03:00
LICENSE feat(docs): README edited to represent the current JS Client API (#256) 2023-02-16 17:45:37 +03:00
package.json feat: update copyright (#449) 2024-05-16 12:34:31 +03:00
pnpm-lock.yaml fix(deps): use external fluence-network-environment 1.2.2 [fixes FLU-689] (#454) 2024-07-31 14:52:10 +03:00
pnpm-workspace.yaml fix(tests): Repair integration tests [fixes DXJ-506] (#364) 2023-10-18 08:38:49 +07:00
README.md fix(ci,tests): remove nox image override (#455) 2024-08-09 15:04:52 +03:00
reset.d.ts feat: update copyright (#449) 2024-05-16 12:34:31 +03:00
tsconfig.eslint.json feat(js-client)!: Segregation of responsibility between js-client packages [fixes DXJ-525] (#378) 2023-11-19 09:04:10 +07:00
tsconfig.json feat(npm-aqua-compiler): create package (#401) 2023-12-15 23:14:07 +07:00

Fluence JS Client

js-client @ npm

This is the Javascript client for the Fluence network. The main role of the JS client is to connect to the Fluence Network and allow you to integrate Aqua code into your application.

Installation

JS Client only supports the ESM format that means not every Node.js project can install it. You can read more here

  1. Install the client:

    npm i @fluencelabs/js-client
    
  2. Add the following lines at the beginning of your code:

    import { Fluence, randomKras } from "@fluencelabs/js-client";
    
    Fluence.connect(randomKras());
    

HTML page

Add a script tag with the JS Client module to your index.html. The easiest way to do this is using a CDN ( like JSDELIVR or UNPKG).

Here is an example using the JSDELIVR CDN:

<head>
  <title>Cool App</title>
  <script type="module">
    import {
      Fluence,
      randomKras,
    } from "https://cdn.jsdelivr.net/npm/@fluencelabs/js-client/dist/browser/index.min.js";

    Fluence.connect(randomKras());
  </script>
</head>

If you cannot or don't want to use a CDN, feel free to get the script directly from the npm package and host it yourself. You can find the script in the /dist/browser directory of the package. (Note: this option means that developers understand what they are doing and know how to serve this file from their own web server.)

Usage in an Application

Once you've added the client, you can compile Aqua and run it in your application. To compile Aqua, use Fluence CLI.

  1. Install the package:

    npm i -D @fluencelabs/cli
    
  2. Add a directory in your project for Aqua code, e.g., _aqua.

  3. Put *.aqua files in that directory.

  4. Add a directory for compiled Aqua files inside your sources. For example, if your app source is located in the src directory, you can create src/_aqua.

  5. To compile Aqua code once, run npx fluence aqua -i ./_aqua -o ./src/_aqua/. To watch the changes and to recompile on the fly, add the -w flag: npx fluence aqua -w -i ./_aqua -o ./src/_aqua/.

    Hint: it might be a good idea to add these scripts to your package.json file. For example, you project structure could look like this:

     ┣ _aqua
     ┃ ┗ demo.aqua
     ┣ src
     ┃ ┣ _aqua
     ┃ ┃ ┗ demo.ts
     ┃ ┗ index.ts
     ┣ package-lock.json
     ┣ package.json
     ┗ tsconfig.json
    

    Then, your package.json file should include the following lines:

    {
      ...
      "scripts": {
        ...
        "aqua:compile": "fluence aqua -i ./aqua/ -o ./src/_aqua",
        "aqua:watch": "fluence aqua -w -i ./aqua/ -o ./src/_aqua"
      },
      ...
    }
    
  6. Now you can import and call Aqua code from your application like this:

    import { getRelayTime } from "./_aqua/demo";
    
    async function buttonClick() {
      const time = await getRelayTime();
      alert("relay time: " + time);
    }
    

Debug

JS Client uses the debug library under the hood for logging. The log namespaces are structured on a per-component basis, following this structure:

fluence:<component>:trace
fluence:<component>:debug
fluence:<component>:error

Marine JS logs have a slightly different structure:

fluence:marine:<service id>:trace
fluence:marine:<service id>:debug
fluence:marine:<service id>:info
fluence:marine:<service id>:warn
fluence:marine:<service id>:error

Each level corresponds to a logging level in Marine JS.

Star (*) character can be used as a wildcard to enable logs for multiple components at once. For example, DEBUG=fluence:* will enable logs for all components. To exclude a component, use a minus sign before the component name. For example, DEBUG=fluence:*,-fluence:particle:*

Index of components:

  • particle: everything related to particle processing queue
  • aqua: infrastructure of aqua compiler support
  • connection: connection layer
  • marine: Marine JS logs

Enabling logs in Node.js

Enable logs by passing the environment variable DEBUG with the corresponding log level. For example:

DEBUG=fluence:* node --loader ts-node/esm ./src/index.ts

Enabling logs in the browser

To enable logs, set the localStorage.debug variable. For example:

localStorage.debug = "fluence:*";

NOTE

In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScript console will be default—only to show messages logged by debug if the "Verbose" log level is enabled.

Low level usage

JS client also has an API for low level interaction with AVM and Marine JS. It could be handy in advanced scenarios when a user fetches AIR dynamically or generates AIR without default Aqua compiler.

callAquaFunction Allows to call aqua function without schema.

registerService Gives an ability to register service without schema. Passed service could be

  • Plain object. In this case all function properties will be registered as AIR service functions.
  • Class instance. All class methods without inherited ones will be registered as AIR service functions.

Development

To hack on the Fluence JS Client itself, please refer to the development page.

Documentation

The starting point for all documentation related to Fluence is fluence.dev. We also have an active YouTube channel.

Support

Please, file an issue if you find a bug. You can also contact us at Discord or Telegram. We will do our best to resolve the issue ASAP.

Contributing

Any interested person is welcome to contribute to the project. Please, make sure you read and follow some basic rules.

License

All software code is copyright (c) Fluence DAO, Inc. under the Apache-2.0 license.