Hedera
Search…
Get topic messages
Subscribe to a topic ID's messages from a mirror node. You will receive all messages for the specified topic or within the defined start and end time.
V2
V1
Constructor
Description
new TopicMessageQuery()
Initializes the TopicMessageQuery object
1
new TopicMessageQuery()
Copied!

Methods

Method
Type
Description
Requirement
setTopicId(<topicId>)
TopicId
The topic ID to subscribe to
Required
setStartTime(<startTime>)
Instant
The time to start subscribing to a topic's messages
Optional
setEndTime(<endTime>)
Instant
The time to stop subscribing to a topic's messages
Optional
setLimit(<limit>)
long
The number of messages to return
Optional
subscribe(<client, onNext)
SubscriptionHandle
Client, Consumer<TopicMessage>
Required
Java
1
//Create the query
2
new TopicMessageQuery()
3
.setTopicId(newTopicId)
4
.subscribe(client, topicMessage -> {
5
System.out.println("at " + topicMessage.consensusTimestamp + " ( seq = " + topicMessage.sequenceNumber + " ) received topic message of " + topicMessage.contents.length + " bytes");
6
});
7
​
8
//v2.0.0
Copied!
JavaScript
1
//Create the query
2
new TopicMessageQuery()
3
.setTopicId(topicId)
4
.setStartTime(0)
5
.subscribe(
6
client,
7
(message) => console.log(Buffer.from(message.contents, "utf8").toString())
8
);
9
//v2.0.0
Copied!
Go
1
//Create the query
2
_, err = hedera.NewTopicMessageQuery().
3
SetTopicID(topicID).
4
Subscribe(client, func(message hedera.TopicMessage) {
5
if string(message.Contents) == content {
6
wait = false
7
}
8
})
9
​
10
if err != nil {
11
panic(err)
12
}
13
​
14
//v2.0.0
Copied!
Constructor
Description
new MirrorConsensusTopicQuery()
Initializes the MirrorConsensusTopicQuery object
1
new MirrorConsensusTopicQuery()
Copied!

Methods

Method
Type
Description
Requirement
setTopicId(<topicId>)
TopicId
The topic ID to subscribe to
Required
setStartTime(<startTime>)
Instant
The time to start subscribing to a topic's messages
Optional
setEndTime(<endTime>)
Instant
The time to stop subscribing to a topic's messages
Optional
setLimit(<limit>)
long
The number of messages to return
Optional
subscribe(<mirrorClient, onNext onError)
MirrorClient, Consumer <MirrorConsensusTopicResponse>, Consumer<Throwable>
Subscribe and get the messages for a topic
Required
Java
1
new MirrorConsensusTopicQuery()
2
.setTopicId(topicId)
3
.subscribe(mirrorClient, resp -> {
4
String messageAsString = new String(resp.message, StandardCharsets.UTF_8);
5
​
6
System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
7
},
8
// On gRPC error, print the stack trace
9
Throwable::printStackTrace);
10
//v1.3.2
Copied!
JavaScript
1
new MirrorConsensusTopicQuery()
2
.setTopicId(topicId)
3
.subscribe(
4
consensusClient,
5
(message) => console.log(message.toString()),
6
(error) => console.log(`Error: ${error}`)
7
);
Copied!
Last modified 3mo ago
Copy link