Connect a client
This section covers how to connect a frontend or other client to a dapp backend deployed on a Chromia network. The
examples here use the TypeScript-based postchain-client
, but the
principles are the same when using other client libraries.
To connect to a deployed dapp, the client library needs a pool of URLs to system nodes in the Chromia network, as well as the Blockchain RID of the dapp to connect to. The client automatically queries the Directory chain on the system nodes to fetch the URLs of all nodes where your dapp is currently running, thus establishing a connection to the dapp.
-
Ensure you have a dapp deployed on a Chromia network. This will most likely be ProjectNet or HackNet. For deployment instructions, see Deploy your dapp to testnet.
-
If you are deploying a frontend created using the Hello World guide, remove the admin key pair configuration and replace it with a secure link to your stored key pair. This ensures the security of your key pair. For more details on how to build a production-ready dapp, visit the Chromia course page.
// Remove any private keys from your production client code
const adminPubkey = Buffer.from("<PubkeyLink>", "hex");
const adminPrivkey = Buffer.from("<PrivkeyLink>", "hex"); -
Specify URLs of Chromia nodes running the Directory chain in your client code. This varies depending on which network your dapp is deployed to.
infoTo configure your client for a dapp running on ProjectNet, use the following system node URLs:
const directoryNodeUrlPool = [
"https://node0.projectnet.chromia.dev:7740",
"https://node1.projectnet.chromia.dev:7740",
"https://node2.projectnet.chromia.dev:7740",
"https://node3.projectnet.chromia.dev:7740",
];To configure your client for a dapp running on HackNet, use the following system node URLs:
const directoryNodeUrlPool = [
"https://node0.hacknet.chromia.dev:7740",
"https://node1.hacknet.chromia.dev:7740",
"https://node2.hacknet.chromia.dev:7740",
"https://node3.hacknet.chromia.dev:7740",
];If you're using a different network, replace the value of
directoryNodeUrlPool
with the appropriate URLs. -
Establish a connection to your dapp by providing the dapp's Blockchain RID and the system node URLs:
// Connection input
const blockchainRID = "<BlockchainRID>"; // Target Blockchain RID
const directoryNodeUrlPool = ["<TargetUrl1>", "<TargetUrl2>", "etc."]; // Target URLs
// Connection setup
const chromiaClient = await pcl.creatClient({
blockchainRID,
directoryNodeUrlPool,
});Replace
<BlockchainRID>
with the actual blockchain RID for your dapp, and the<TargetUrls>
with the appropriate Chromia system node URLs from step 3.
With these steps, you have successfully configured postchain-client
to work with your dapp backend running on the
Chromia network.
To learn more about developing production-ready dapps, visit the Chromia course page.