2023-02-16 14:45:37 +00:00
# Fluence JS Client
2020-12-28 14:39:06 +00:00
2023-02-16 14:45:37 +00:00
[![npm ](https://img.shields.io/npm/v/@fluencelabs/js-client.api?label=@fluencelabs/js-client.api )](https://www.npmjs.com/package/@fluencelabs/js-client.api)
[![npm ](https://img.shields.io/npm/v/@fluencelabs/js-client.web.standalone?label=@fluencelabs/js-client.web.standalone )](https://www.npmjs.com/package/@fluencelabs/js-client.web.standalone)
2020-12-28 14:39:06 +00:00
2023-02-21 08:38:26 +00:00
This is the Javascript client for the [Fluence ](https://fluence.network ) 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.
2021-01-29 13:48:27 +00:00
2023-02-21 08:38:26 +00:00
## Installation
2021-01-29 13:48:27 +00:00
2023-02-21 08:38:26 +00:00
Adding the Fluence JS client for your web application is very easy.
2020-05-14 12:20:39 +00:00
2023-02-21 08:38:26 +00:00
### Browser-based Apps
2023-02-16 14:45:37 +00:00
1. Add a script tag with the JS Client bundle to your `index.html` . The easiest way to do this is using a CDN (like [JSDELIVR ](https://www.jsdelivr.com/ ) or [UNPKG ](https://unpkg.com/ )). The script is large, thus we highly recommend to use the `async` attribute.
Here is an example using the JSDELIVR CDN:
```html
< head >
2023-02-21 08:38:26 +00:00
< title > Cool App< / title >
< script
src="https://cdn.jsdelivr.net/npm/@fluencelabs/js-client.web.standalone@0.13.3/dist/js-client.min.js"
async
>< / script >
2023-02-16 14:45:37 +00:00
< / head >
```
2023-02-21 08:38:26 +00:00
If you cannot or don't want to use a CDN, feel free to get the script directly from the [npm package ](https://www.npmjs.com/package/@fluencelabs/js-client.web.standalone ) and host it yourself. You can find the script in the `/dist` 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.)
2023-02-16 14:45:37 +00:00
2. Install the following packages:
```
npm i @fluencelabs/js -client.api @fluencelabs/fluence -network-environment
```
3. Add the following lines at the beginning of your code:
```
import { Fluence } from "@fluencelabs/js-client.api";
2023-02-21 08:38:26 +00:00
import { randomKras } from '@fluencelabs/fluence-network-environment';
2023-02-16 14:45:37 +00:00
2023-02-21 08:38:26 +00:00
Fluence.connect(randomKras());
2023-02-16 14:45:37 +00:00
```
2023-02-21 08:38:26 +00:00
### Node.js Apps
2023-02-23 17:43:19 +00:00
**Prerequisites:**
The Fluence JS Client only supports the ESM format. This implies that a few preliminary steps are required if your project is not already using ESM:
- Add `"type": "module"` to your package.json.
- Replace `"main": "index.js"` with `"exports": "./index.js"` in your package.json.
- Remove `'use strict';` from all JavaScript files.
- Replace all `require()` /`module.export` with `import` /`export`.
- Use only full relative file paths for imports: `import x from '.';` → `import x from './index.js';` .
If you are using TypeScript:
- Make sure you are using TypeScript 4.7 or later.
- Add [`"module": "ESNext", "target": "ESNext", "moduleResolution": "nodenext"` ](https://www.typescriptlang.org/tsconfig#module ) to your tsconfig.json.
- Use only full relative file paths for imports: `import x from '.';` → `import x from './index.js';` .
- Remove `namespace` usage and use `export` instead.
- You must use a `.js` extension in relative imports even though you're importing `.ts` files.
**Installation:**
2023-02-21 08:38:26 +00:00
1. Install the following packages:
```
npm i @fluencelabs/js -client.api"@fluencelabs/js-client.node @fluencelabs/fluence -network-environment
```
2. Add the following lines at the beginning of your code:
```
import '@fluencelabs/js-client.node';
import { Fluence } from "@fluencelabs/js-client.api";
import { randomKras } from '@fluencelabs/fluence-network-environment';
Fluence.connect(randomKras());
```
## Usage in an Application
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
Once you've added the client, you can compile [Aqua ](https://github.com/fluencelabs/aqua ) and run it in your application. To compile Aqua, use [Fluence CLI ](https://github.com/fluencelabs/fluence-cli ).
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
1. Install the package:
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
```
npm i -D "@fluencelabs/fluence-cli"
```
2022-08-24 15:03:06 +00:00
2023-02-16 14:45:37 +00:00
2. Add a directory in your project for Aqua code, e.g., `_aqua` .
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
3. Put `*.aqua` files in that directory.
2022-08-24 15:03:06 +00:00
2023-02-23 08:58:40 +00:00
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` .
2022-08-24 15:03:06 +00:00
2023-02-16 14:45:37 +00:00
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/` .
2022-08-24 15:03:06 +00:00
2023-02-23 08:58:40 +00:00
**Hint** : it might be a good idea to add these scripts to your `package.json` file.
2023-02-16 14:45:37 +00:00
For example, you project structure could look like this:
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
```
┣ _aqua
┃ ┗ demo.aqua
┣ src
┃ ┣ _aqua
┃ ┃ ┗ demo.ts
┃ ┗ index.ts
┣ package-lock.json
┣ package.json
┗ tsconfig.json
```
2021-01-29 13:48:27 +00:00
2023-02-16 14:45:37 +00:00
Then, your `package.json` file should include the following lines:
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
```
{
...
"scripts": {
...
"aqua:compile": "fluence aqua -i ./aqua/ -o ./src/_aqua",
"aqua:watch": "fluence aqua -w -i ./aqua/ -o ./src/_aqua"
},
...
}
```
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
6. Now you can import and call Aqua code from your application like
this:
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
```
import { getRelayTime } from "./_aqua/demo";
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
async function buttonClick() {
const time = await getRelayTime();
alert("relay time: " + time);
}
```
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
## Development
To hack on the Fluence JS Client itself, please refer to the [development page ](./DEVELOPING.md ).
## Documentation
The starting point for all documentation related to Fluence is
[fluence.dev ](https://fluence.dev/ ). We also have an active [YouTube channel ](https://www.youtube.com/@fluencelabs ).
## Support
2023-02-21 08:38:26 +00:00
Please, file an [issue ](https://github.com/fluencelabs/js-client/issues ) if you find a bug. You can also contact us at [Discord ](https://discord.com/invite/5qSnPZKh7u ) or [Telegram ](https://t.me/fluence_project ). We will do our best to resolve the issue ASAP.
2023-02-16 14:45:37 +00:00
## Contributing
2021-02-25 12:33:37 +00:00
2023-02-16 14:45:37 +00:00
Any interested person is welcome to contribute to the project. Please, make sure you read and follow some basic [rules ](./CONTRIBUTING.md ).
2021-01-29 13:48:27 +00:00
## License
2023-02-16 14:45:37 +00:00
All software code is copyright (c) Fluence Labs, Inc. under the [Apache-2.0 ](./LICENSE ) license.