Aggregate blockchain data with Klayr Service
Klayr Service is a web application middleware that allows interaction with various blockchain networks based on the Klayr protocol.
Data delivery to UI clients like Klayr Desktop, or a custom frontend like Hello Frontend is the primary goal of Klayr Service. Klayr Service offers users a lot more information and endpoints as compared to node APIs, including geolocation and different statistics about network utilization. In addition, Klayr Service makes it possible to access all the live blockchain data such as the Klayr SDK API.
In this guide, you’ll learn to connect Klayr Service to a blockchain node. Klayr Service is installed in addition to a Klayr application such as Klayr Core or a blockchain client of a sidechain, to provide enriched network data for third-party services.
Pre-requisite
|
1. Enable API on the blockchain node
We recommend using PM2 for running a node with the desired configurations.
-
--api-ws
enables the WebSocket API of the node, if not already enabled within the config.json file. -
--api-ws-host=0.0.0.0
allows remote servers to connect to the blockchain node via WS API. If this option is not set, it defaults to127.0.0.1
. -
--api-ws-port=7887
is the port for the WebSocket API. -
--api-http
allows remote servers to connect with the blockchain node via HTTP. -
--network
or-n
flag determines the blockchain network a node connects to. To connect to a specific network, mention either one ofmainnet
,default
,testnet
, ordevnet
with the network flag.
klayr-core start --network mainnet --api-ws --api-ws-host=0.0.0.0 --api-ws-port=7887 --api-http
While using pm2.conf.json for running a node, the starting script can look like the following:
{
"apps": [
{
"name": "hello_client",
"script": "./bin/run start --api-ws -n default --api-ws-host=0.0.0.0 --api-ws-port=7887 --api-http"
}
]
}
The script assumes that the blockchain client has been built with npm run build and the pm2.conf.json assumes that it lies within the blockchain client’s root directory.
|
To start the node with your desired configurations mentioned above, execute the following command:
pm2 start pm2.conf.json
Once the node starts, follow the steps mentioned in the Connecting Klayr Service to a blockchain node section to connect Klayr Service with a node.
2. Running Klayr Service
Run the docker container for klayr-service
, redis
, and mysql
or mariadb
. It is recommended to use mariadb
instead of mysql
when running on Darwin.
-
Run Klayr Service container:
./klayr-service/make up
-
Run Redis container:
./klayr-service/jenkins/redismake up
-
Run MariaDB container:
./klayr-service/jenkins/mariadbmake up
If you installed Klayr Service via source then, follow the steps mentioned in the Source setup guide to run Klayr Service. |
3. Checking established connection
After both the blockchain client and the Klayr Service are running and connected, you should see a similar entry in the logs of the blockchain node.
2023-06-27T11:03:39.085Z INFO XYZ.local engine 35439 New web socket client connected
Another hint that Klayr Service is now connected to your node is the occurrence of the following entry in the logs from the blockchain-connector
service.
2023-06-26T16:09:28.435 INFO [app] Found a node, starting service KLAYR-SERVICE-BLOCKCHAIN-CONNECTOR...
You can also invoke an endpoint of the blockchain node via Klayr service API like the following, to verify that the connection was established successfully.
curl --location 'http://localhost:9901/api/v3/invoke' --header 'Content-Type: application/json' --data '{"endpoint": "chain_getLastBlock","params": {}}' | json_pp
You can now customize your UI to take advantage of all the enriched APIs of Klayr Service.
For more information about Klayr Service APIs, see the following pages:
To learn more about extending Klayr Service, see Extending the Indexer microservice.