This tutorial will take you through the process of preparing and posting comment using the broadcast.comment operation.
Being able to post a comment is critical to most social applications built on Hive.
Intro
We are using the broadcast.comment function provided by the dhive library which generates, signs, and broadcasts the transaction to the network. On the Hive platform, posts and comments are all internally stored as a comment object, differentiated by whether or not a parent_author exists. When there is no parent_author, the it’s a post, when there is, it’s a comment. An account can broadcast a comment on the blockchain every 3 seconds (with every new block) enabling the user to comment as they wish with almost no wait time between commits.
As usual, we have a public/app.js file which holds the Javascript segment of the tutorial. In the first few lines we define the configured library and packages:
constdhive=require('@hiveio/dhive');letopts={};//connect to community testnetopts.addressPrefix='TST';opts.chainId='18dcf0a285365fc58b71f18b3d3fec954aa0c141c44e4e5cb4cf777b9eab274e';//connect to server which is connected to the network/testnetconstclient=newdhive.Client('http://127.0.0.1:8090',opts);
Above, we have dhive pointing to the test network with the proper chainId, addressPrefix, and endpoint.
Because this tutorial modifies the blockchain, we will use a testnet and a predefined account to demonstrate comment publishing.
2. Choose parent post
We need to choose a parent post and parse out the parent author and parent permlink.
Below is a url that uses an instance of condenser pointed at our testnet.
Every post needs a unique permalink.
Because comments don’t typically have a title, we recommend using a random number for ours.
Note: Take care of your users: Because permlinks are unique within an author’s scope, we recommend random numbers for comments; or at least making it a default in your settings.
//generate random permanent link for postconstpermlink=Math.random().toString(36).substring(2);
6. Build comment object
We take the information we gathered from the UI and put it into a well structured comment object.
We post the comment to the blockchain and render the resulting block number if successful,
or output an error to the console if there’s a failure.
console.log('comment broadcast object',comment);client.broadcast.comment(comment,privateKey).then(function(result){console.log('comment broadcast result',result);document.getElementById('postLink').innerHTML=`<br/><p>Included in block: ${result.block_num}</p><br/><br/><a href="http://127.0.0.1:8080/@${parent_author}/${parent_permlink}">Check post here</a>`;},function(error){console.error(error);});
A successful comment will output something like the following to the console:
That’s all there is to it.
The broadcast operation has more to offer than just committing a post/comment to the blockchain. It provides a mulititude of options that can accompany this commit. The max payout and percent of hive dollars can be set. When authors don’t want all of the benifits from a post, they can set the payout factors to zero or beneficiaries can be set to receive part of the rewards. You can also set whether votes are allowed or not. The broadcast to the blockchain can be modified to meet the exact requirements of the author. More information on how to use the broadcast operation can be found on the Hive Devportal with a list of the available broadcast options under the specific Appbase API
Final code:
//Step 1.import{Client,PrivateKey}from'@hiveio/dhive';import{TestnetasNetConfig}from'../../configuration';//A Hive Testnet. Replace 'Testnet' with 'Mainnet' to connect to the main Hive blockchain.letopts={...NetConfig.net};//connect to server which is connected to the network/testnetconstclient=newClient(NetConfig.url,opts);//Step 2. user fills in the values for 'parent_author' and 'parent_permlink'//Step 3. user adds content for the comment in the 'body' textarea//submit post functionwindow.submitComment=async()=>{//Step 4. get all values from the UI//get private keyconstprivateKey=PrivateKey.fromString(document.getElementById('postingKey').value);//get account nameconstaccount=document.getElementById('username').value;//get bodyconstbody=document.getElementById('body').value;//get parent author permalinkconstparent_author=document.getElementById('parent_author').value;//get parent author permalinkconstparent_permlink=document.getElementById('parent_permlink').value;//generate random permanent link for postconstpermlink=Math.random().toString(36).substring(2);constpayload={author:account,title:'',body:body,parent_author:parent_author,parent_permlink:parent_permlink,permlink:permlink,json_metadata:'',};console.log('client.broadcast.comment payload:',payload);client.broadcast.comment(payload,privateKey).then(function(result){console.log('client.broadcast.comment response',result);document.getElementById('postLink').style.display='block';document.getElementById('postLink').innerHTML=`<br/><p>Included in block: ${result.block_num}</p><br/><br/><a href="http://127.0.0.1:8080/@${parent_author}/${parent_permlink}">Check post here</a>`;},function(error){console.error(error);});};window.clearFields=function(){document.getElementById('body').value='';document.getElementById('parent_author').value='';document.getElementById('parent_permlink').value='';};window.onload=()=>{constaccount=NetConfig.accounts[0];document.getElementById('username').value=account.address;document.getElementById('postingKey').value=account.privPosting;};
Submit Comment Reply
How to post a simple comment to Hive.
Full, runnable src of Submit Comment Reply can be downloaded as part of: tutorials/javascript (or download just this tutorial: devportal-master-tutorials-javascript-11_submit_comment_reply.zip).
This tutorial will take you through the process of preparing and posting comment using the
broadcast.comment
operation. Being able to post a comment is critical to most social applications built on Hive.Intro
We are using the
broadcast.comment
function provided by thedhive
library which generates, signs, and broadcasts the transaction to the network. On the Hive platform, posts and comments are all internally stored as acomment
object, differentiated by whether or not aparent_author
exists. When there is noparent_author
, the it’s a post, when there is, it’s a comment. An account can broadcast a comment on the blockchain every 3 seconds (with every new block) enabling the user to comment as they wish with almost no wait time between commits.Also see:
Steps
dhive
intoapp.js
and prepare it to communicate with a Hive blockchainbody
content to your comment1. App setup
You can launch a local testnet, with port 8090 mapped locally to the docker container:
For details on running a local testnet, see: Setting Up a Testnet
As usual, we have a
public/app.js
file which holds the Javascript segment of the tutorial. In the first few lines we define the configured library and packages:Above, we have
dhive
pointing to the test network with the proper chainId, addressPrefix, and endpoint.Because this tutorial modifies the blockchain, we will use a testnet and a predefined account to demonstrate comment publishing.
2. Choose parent post
We need to choose a parent post and parse out the parent author and parent permlink. Below is a url that uses an instance of condenser pointed at our testnet.
In this case.
dsf0yxlox2d
will be our parent permlink and@demo
will be the the parent author.3. Add content
We’ve added the parent post info and
Some amazing content
in our UI via the keyboard.4. Get Comment Data
In the
submitComment
function, (runs when “Submit comment!” is clicked) We gather information from the UI.5. Create comment permlink
Every post needs a unique permalink. Because comments don’t typically have a title, we recommend using a random number for ours.
Note: Take care of your users: Because permlinks are unique within an author’s scope, we recommend random numbers for comments; or at least making it a default in your settings.
6. Build comment object
We take the information we gathered from the UI and put it into a well structured comment object.
7. Post comment
We post the comment to the blockchain and render the resulting block number if successful, or output an error to the console if there’s a failure.
A successful comment will output something like the following to the console:
That’s all there is to it.
The
broadcast
operation has more to offer than just committing a post/comment to the blockchain. It provides a mulititude of options that can accompany this commit. The max payout and percent of hive dollars can be set. When authors don’t want all of the benifits from a post, they can set the payout factors to zero or beneficiaries can be set to receive part of the rewards. You can also set whether votes are allowed or not. The broadcast to the blockchain can be modified to meet the exact requirements of the author. More information on how to use thebroadcast
operation can be found on the Hive Devportal with a list of the available broadcast options under the specific Appbase APIFinal code:
To Run the tutorial
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/javascript/11_submit_comment_reply
npm i
npm run dev-server
ornpm run start