HCS: Topic
Hello World sequence: Create a new topic Hedera Consensus Service (HCS), and publish a message to this topic.
What you will accomplish
Prerequisites
Before you begin, you should have completed the following Hello World sequence:
Get started: Set up project
To follow along, start with the main
branch, which is the default branch of the repo. This gives you the initial state from which you can follow along with the steps as described in the tutorial.
You should already have this from the "Create and Fund Account" sequence. If you have not completed this, you are strongly encouraged to do so.
Alternatively, you may wish to create a .env
file and populate it as required.
In the terminal, from the hello-future-world
directory, enter the subdirectory for this sequence.
Reuse the .env
file by copying the one that you have previously created into the directory for this sequence.
Next, install the dependencies using npm
.
Then open the script-hcs-topic.js
file in a code editor, such as VS Code.
Write the script
An almost complete script has already been prepared for you, script-hcs-topic.js
, and you will only need to make a few modifications (outlined below) to run successfully.
Step 1: Create a topic
To create a new HCS topic, submit a TopicCreateTransaction
, from the Hedera SDK. This transaction does not need any input parameters.
Note that once this transaction has been completed, it is important to extract the topicID from it, as you will need this to submit messages to it. This has already been done, and no modification is necessary.
Step 2: Publish message to topic
Once the topic has been registered, you're ready to submit messages to it. Messages may be any data up to 1024 bytes.
To do so, submit a TopicMessageSubmitTransaction
from the Hedera SDK. This transaction needs 2 input parameters, topicId
and message
. Use the topicId
obtained from the receipt of the TopicCreateTransaction
submitted earlier. For message
, use a string Hello HCS -
followed by your name (or nickname). For example, if you used bguiz
, the string should be Hello HCS - bguiz
.
Run the script
In the terminal, run the script using the following command:
You should see output similar to the following:
To verify that both the TopicCreateTransaction
and TopicMessageSubmitTransaction
have worked, check Hashscan (the network explorer). To do so, open topicExplorerUrl
in your browser and check that:
The topic exists, and its topic ID matches
topicId
output by the script. (1)There is one entry in the topic, and its message is
Hello HCS -
followed by your name/ nickname. (2)
Complete
Congratulations, you have completed the Hedera Consensus Service Hello World sequence! 🎉🎉🎉
You have learned how to:
Next Steps
Now that you have completed this Hello World sequence, you have interacted with Hedera Consensus Service (HCS). There are other Hello World sequences for Hedera Smart Contract Service (HSCS), Hedera File Service (HFS), and Hedera Token Service (HTS), which you may wish to check out next.
Cheat sheet
Writer: Brendan
Last updated