2023-02-15 14:56:32 +04:00
# Fluence JS Client
2020-12-28 17:39:06 +03:00
2023-02-15 16:04:50 +04:00
[](https://www.npmjs.com/package/@fluencelabs/js -client.api)
[](https://www.npmjs.com/package/@fluencelabs/js -client.web.standalone)
2020-12-28 17:39:06 +03:00
2023-02-15 14:56:32 +04:00
Javascript client to Fluence network
## WARNING
Client for NodeJS is broken. We will fix that shortly.
2021-01-29 16:48:27 +03:00
## Getting started
2023-02-15 14:56:32 +04:00
Adding JS Client for your web application is very easy
2020-05-14 15:20:39 +03:00
2023-02-15 14:56:32 +04:00
1. Pick you favorite framework
2. Add a script tag with the JS Client bundle to your index.html. The easiest way to do so is using CDN (like https://www.jsdelivr.com/ or https://unpkg.com/) The script is large thus we highly recommend to use `async` attribute
2021-01-29 16:48:27 +03:00
2023-02-15 14:56:32 +04:00
Here is an example using React App and jsdelivr cdn.
2021-01-29 16:48:27 +03:00
2023-02-15 14:56:32 +04:00
```
< title > React App< / title >
< script src = 'https://cdn.jsdelivr.net/npm/ @fluencelabs/js -client.web.standalone @0 .10.0/dist/js-client.min.js'
async>< / script >
< / head >
```
2021-01-29 16:48:27 +03:00
2023-02-15 14:56:32 +04:00
If you can't or don't want to use CDN, feel free to get the script directly from the npm package and host in yourself: `https://www.npmjs.com/package/@fluencelabs/js-client.web.standalone` . You can find the script in `/dist` directory of the package. (note: that options means that the developer understands what he's doing and he knows to serve this file from his own web server)
2022-08-24 18:03:06 +03:00
2023-02-15 14:56:32 +04:00
3. Install the following packages:
2021-01-29 16:48:27 +03:00
2023-02-15 14:56:32 +04:00
```
npm i @fluencelabs/js -client.api @fluencelabs/fluence -network-environment
2022-08-24 18:03:06 +03:00
```
2023-02-15 14:56:32 +04:00
4. In the beginning of your app do the following:
2022-08-24 18:03:06 +03:00
2021-01-29 16:48:27 +03:00
```
2023-02-15 14:56:32 +04:00
import { Fluence } from "@fluencelabs/js -client.api";
import { krasnodar } from "@fluencelabs/fluence -network-environment";
Fluence.start({
relay: krasnodar[3],
});
2022-08-24 18:03:06 +03:00
```
2023-02-15 14:56:32 +04:00
## Use aqua in web application
2021-01-29 16:48:27 +03:00
2023-02-15 14:56:32 +04:00
Once you've added the client you can compile aqua and run it in your application.
2021-01-29 16:48:27 +03:00
2023-02-15 15:52:06 +04:00
To compile aqua use Fluence CLI: https://github.com/fluencelabs/fluence-cli
1. Install package:
```
npm i -D "@fluencelabs/fluence -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 you sources. E.G if you app source are located in `src` folder, you can create `src/_aqua`
5. To compile aqua code once run `npx fluence aqua -i ./_aqua -o ./src/_aqua/`
To watch for changes and recompile on the fly add `-w` flag `npx fluence aqua -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:
```
2023-02-15 15:55:38 +04:00
┣ _aqua
┃ ┗ demo.aqua
2023-02-15 15:52:06 +04:00
┣ src
┃ ┣ _aqua
2023-02-15 15:55:38 +04:00
┃ ┃ ┗ demo.ts
2023-02-15 15:52:06 +04:00
┃ ┗ index.ts
┣ package-lock.json
┣ package.json
┗ tsconfig.json
```
And your package json:
```
{
...
"scripts": {
...
"aqua:compile": "fluence aqua -i ./aqua/ -o ./src/_aqua",
"aqua:watch": "fluence aqua -w -i ./aqua/ -o ./src/_aqua"
},
...
}
```
2021-02-25 15:33:37 +03:00
2023-02-15 15:55:38 +04:00
6. Now you can import and use call aqua code from your application. e.g;
```
import { getRelayTime } from "./_aqua/demo";
async function buttonClick() {
const time = await getRelayTime();
alert("relay time: " + time);
}
```
2023-02-15 14:56:32 +04:00
## Developing
2021-02-25 15:33:37 +03:00
2023-02-15 14:56:32 +04:00
To hack on the JS Client itself refer to [dev page ](./DEVLOPING.md ).
2021-02-25 15:33:37 +03:00
2023-02-15 14:56:32 +04:00
## Support
2021-02-25 15:33:37 +03:00
2023-02-15 14:56:32 +04:00
Please, file an [issue ](https://github.com/fluencelabs/fluence-js/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.
2021-02-25 15:33:37 +03:00
2023-02-15 14:56:32 +04:00
## Contributing
2021-02-25 15:33:37 +03:00
2023-02-15 14:56:32 +04: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 16:48:27 +03:00
## License
2023-02-15 14:56:32 +04:00
All software code is copyright (c) Fluence Labs, Inc. under the [Apache-2.0 ](./LICENSE ) license.