Fluence client library is designed to make it simple to send requests to the Fluence apps. You don't need to know anything about the internals of Fluence, just use it like other common libraries.
npm install --save fluence
We need to know three constants to work with Fluence:
// address to Fluence contract in Ethereum blockchain.
let contractAddress = "0xeFF91455de6D4CF57C141bD8bF819E5f873c1A01";
// set ethUrl to `undefined` to use MetaMask instead of Ethereum node
let ethUrl = "http://geth.fluence.one:8545/";
// application to interact with that stored in Fluence contract
let appId = "43";
Let's create a session with Fluence and start sending requests.
import * as fluence from "fluence";
let session;
fluence.connect(contractAddress, appId, ethUrl).then((s) => {
console.log("Session created");
session = s;
});
You should remember that all interactions are asynchronous, so we always work with promises.
Send request to the backend:
let requestId = await s.requestAsync("Hello, Fluence!");
Sending a request with transaction, and getting its result are separate operations, because result appears only after a few blocks, and can't be retrieved immediately. You may also not want to retrieve result for each operations, for performance reasons. For example, when batching data updates, and then retrieving a single result.
So, call query()
to read result of the executed transaction:
request.query(requestId)
.then((r) => console.log(r.asString()))
asString()
method converts hex bytes to to a UTF-8 string.
That's all! Based on this API you can build lots of useful client-server applications.
Sign all requests
You can add private key as a fourth argument in connect
method if it is needed to check the correctness of signature on the side of a backend (authorization).
fluence.connect(contractAddress, appId, ethUrl, privateKey)
fluence.connect(contractAddress, appId, undefined)
let appId = 41 // it is not important on local nodes, could be random number
fluence.directConnect("localhost", 30000, appId);
Debugging
For info on how to run a backend app locally for debugging, read here.
What's Next
Developing a Front-end app |