Subscribe to a topic ID's messages from a mirror node. You will recieve all messages for the specified topic or within the defined start and end time.
Constructor | Description |
| Initializes the TopicMessageQuery object |
new TopicMessageQuery()
Method | Type | Description | Requirement |
| TopicId | The topic ID to subsribe to | Required |
| Instant | The time to start subscribing to a topic's messages | Optional |
| Instant | The time to stop subscribing to a topic's messages | Optional |
| long | The number of messages to return | Optional |
| SubscriptionHandle | Client, Consumer<TopicMessage> | Required |
Java//Create the querynew TopicMessageQuery().setTopicId(newTopicId).subscribe(client, topicMessage -> {System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes");});​//v2.0.0
JavaScript//Create the querynew TopicMessageQuery().setTopicId(topicId).setStartTime(0).subscribe(client,(message) => console.log(Buffer.from(message.contents, "utf8").toString()));//v2.0.0
Go//Create the query_, err = hedera.NewTopicMessageQuery().SetTopicID(topicID).Subscribe(client, func(message hedera.TopicMessage) {if string(message.Contents) == content {wait = false}})​if err != nil {panic(err)}​//v2.0.0
​
Constructor | Description |
| Initializes the MirrorConsensusTopicQuery object |
new MirrorConsensusTopicQuery()
Method | Type | Description | Requirement |
| TopicId | The topic ID to subsribe to | Required |
| Instant | The time to start subscribing to a topic's messages | Optional |
| Instant | The time to stop subscribing to a topic's messages | Optional |
| long | The number of messages to return | Optional |
| MirrorClient, Consumer <MirrorConsensusTopicResponse>, Consumer<Throwable> | Subscribe and get the messages for a topic | Required |
Javanew MirrorConsensusTopicQuery().setTopicId(topicId).subscribe(mirrorClient, resp -> {String messageAsString = new String(resp.message, StandardCharsets.UTF_8);​System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);},// On gRPC error, print the stack traceThrowable::printStackTrace);//v1.3.2
JavaScriptnew MirrorConsensusTopicQuery().setTopicId(topicId).subscribe(consensusClient,(message) => console.log(message.toString()),(error) => console.log(`Error: ${error}`));