Skip to content

Latest commit

 

History

History
166 lines (144 loc) · 5.03 KB

submit-a-message.md

File metadata and controls

166 lines (144 loc) · 5.03 KB

Submit a message

ConsensusMessageSubmitTransaction() submits a message to a topic. If you are trying to submit a message to a private topic, you will need the associated submitKey to successfully submit messages to it.

Constructor Description
ConsensusMessageSubmitTransaction() Initializes a ConsensusMessageSubmitTransaction object
new ConsensusMessageSubmitTransaction()

Basic

Methods Type Description
setTopicID(<topic> TopicID The ID of the topic to submit a message to
setMessage(<message>) byte[ ] The message to submit in byte format. Max size of the transaction (including signatures) is 6144 bytes.
setMessage(<message>) String The message to submit in string format
setMaxChunks(<chunks>) long

Number of transactions to break the entire message into

Default Value: 10

v1.2.0

setChunkInfo(<initaliId, total, number>) TransactionID, int, int

initialId: TransactionID of the first chunk, gets copied to every subsequent chunk in a fragmented message.

total: total number of chunks

number: The sequence number (from 1 to total) of the current chunk in the message.

v1.2.0

Example

{% tabs %} {% tab title="Java" %}

//Submits a message to a public topic 
new ConsensusMessageSubmitTransaction()
    .setTopicId(topicId)
    .setMessage("hello, HCS! " + i)
    .build(client)
    .execute(client)
    .getReceipt(client);

{% endtab %}

{% tab title="JavaScript" %}

//Submits a message to a public topic 
await new ConsensusSubmitMessageTransaction()
    .setTopicId(topicId)
    .setMessage("hello, HCS! " + i)
    .build(client)
    .execute(client)
    .getReceipt(client);

{% endtab %} {% endtabs %}

Advanced

Submit a message to a private topic

To submit a message to a private topic, you will need access to the the submitKey. The submitKey is a property set at the time the topic is created and can be modified. If you do not have the appropriate submitKey, you will not be able to submit messages to that topic.

{% tabs %} {% tab title="Java" %}

new ConsensusMessageSubmitTransaction()
     .setTopicId(topicId)
     .setMessage(message)
     .build(hapiClient)
     // The transaction is automatically signed by the payer.
     // Due to the topic having a submitKey requirement, additionally sign the transaction with that key.
    .sign(submitKey)
    .execute(hapiClient)
    .getReceipt(hapiClient);

{% endtab %}

{% tab title="JavaScript" %}

await new ConsensusSubmitMessageTransaction()
     .setTopicId(topicId)
     .setMessage(message)
     .build(hapiClient)
     // The transaction is automatically signed by the payer.
     // Due to the topic having a submitKey requirement, additionally sign the transaction with that key.
    .sign(submitKey)
    .execute(hapiClient)
    .getReceipt(hapiClient);

{% endtab %} {% endtabs %}

You can view the complete example here.

Submit a message by chunks

A message that is more than 4-6kb can be sent in multiple transactions by chunking the entire message. Use setMaxChunks() in your transaction to designate the number of chunks you would like the message to be broken into.

{% tabs %} {% tab title="Java" %}

// SDK version 1.2.0
// send a message that would fit into more than one chunk (4-6k per chunk)
List<TransactionId> ids = new ConsensusMessageSubmitTransaction()
     .setMaxChunks(5) // this is 10 by default
     .setTopicId(newTopicId)
     .setMessage(bigContents.toString())
     .execute(client);

{% endtab %}

{% tab title="JavaScript" %}

// SDK version 1.2.0
await new ConsensusMessageSubmitTransaction()
     .setTopicId(topicId)
     .setMaxChunks(4) // default: 10
     .setMessage(bigContents)
     .execute(client); //transactionId []

{% endtab %} {% endtabs %}