Code Monkey home page Code Monkey logo

Comments (2)

natanasow avatar natanasow commented on September 18, 2024

Could you try to update to the latest version of hedera-local? I created a sample project and it works like a charm.

Sample

  • index.js is like yours
const { Client, PrivateKey, AccountCreateTransaction, AccountBalanceQuery, Hbar, TopicCreateTransaction, AccountId, TopicMessageQuery, TopicMessageSubmitTransaction } = require("@hashgraph/sdk");

async function main() {
  //Create your local client
  const node = {"127.0.0.1:50211": new AccountId(3)};
  const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600"); //5600 //5551
  client.setOperator(AccountId.fromString("0.0.2"),PrivateKey.fromString("302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137"));

  //Create a new topic
  let txResponse = await new TopicCreateTransaction().execute(client);
  //Get the receipt of the transaction
  let receipt = await txResponse.getReceipt(client);
  //Grab the new topic ID from the receipt
  let topicId = receipt.topicId;

  //Log the topic ID
  console.log(`Your topic ID is: ${topicId}`);

  // Wait 5 seconds between consensus topic creation and subscription
  await new Promise((resolve) => setTimeout(resolve, 5000));

  new TopicMessageQuery()
      .setTopicId(topicId)
      .subscribe(client, null, (message) => {
        let messageAsString = Buffer.from(message.contents, "utf8").toString();
        console.log(`${message.consensusTimestamp.toDate()} Received: ${messageAsString}`);
      });
}
main();
  • msg.js
const {
  Client,
  PrivateKey,
  AccountId,
  TopicMessageSubmitTransaction
} = require("@hashgraph/sdk");

async function main() {
  const node = {"127.0.0.1:50211": new AccountId(3)};
  const client = Client.forNetwork(node).setMirrorNetwork("127.0.0.1:5600");
  client.setOperator(AccountId.fromString("0.0.2"), PrivateKey.fromString("302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137"));

  let sendResponse = await new TopicMessageSubmitTransaction({
    topicId: '0.0.1054', // use the topic id from index.js
    message: "Hello, HCS!",
  }).execute(client);

  const getReceipt = await sendResponse.getReceipt(client);

  const transactionStatus = getReceipt.status;
  console.log("The message transaction status " + transactionStatus);
}

main();

Usage

  • run the index.js
ubuntu@ubuntu:~/sdk-topic-subscribe$ node index.js 
Your topic ID is: 0.0.1054
  • edit the topic id and run the msg.js
ubuntu@ubuntu:~/sdk-topic-subscribe$ node msg.js
The message transaction status 22
  • back to the index.js terminal and the message should be logged
ubuntu@ubuntu:~/sdk-topic-subscribe$ node index.js 
Your topic ID is: 0.0.1054
Thu Aug 25 2022 10:35:20 GMT+0300 (Eastern European Summer Time) Received: Hello, HCS!

from hedera-local-node.

Kalina-Todorova avatar Kalina-Todorova commented on September 18, 2024

Closing this due to inactivity.
@fed-franz Please feel free to reopen or file another issue in case of obstacles.

from hedera-local-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.