Skip to content

Commit

Permalink
refactor: update consensus pub sub example to listen to a topic for 1…
Browse files Browse the repository at this point in the history
…0 minutes
  • Loading branch information
janaakhterov committed Apr 5, 2021
1 parent 1ae3c85 commit 3afb659
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions examples/consensus_pub_sub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,41 @@ func main() {

fmt.Printf("topicID: %v\n", topicID)

wait := true
start := time.Now()

_, err = hedera.NewTopicMessageQuery().
SetTopicID(topicID).
SetStartTime(time.Unix(0, 0)).
Subscribe(client, func(message hedera.TopicMessage) {
if string(message.Contents) == content {
wait = false
}
print("Received message ", message.SequenceNumber, "\r")
})

if err != nil {
println(err.Error(), ": error subscribing to the topic")
return
}

println(transactionResponse.NodeID.String())

_, err = hedera.NewTopicMessageSubmitTransaction().
SetMessage([]byte(content)).
SetTopicID(topicID).
Execute(client)
if err != nil {
println(err.Error(), ": error submitting topic")
return
}

//println(string(messageQuery.GetMessage()))
for {
_, err = hedera.NewTopicMessageSubmitTransaction().
SetMessage([]byte(content)).
SetTopicID(topicID).
Execute(client)

//messageQuery.Execute(client)
if err != nil {
println(err.Error(), ": error submitting topic")
return
}

for {
if !wait || uint64(time.Since(start).Seconds()) > 30 {
break
}
if uint64(time.Since(start).Seconds()) > 60 * 10 {
break;
}

time.Sleep(2500)
time.Sleep(2000)
}

println()

transactionResponse, err = hedera.NewTopicDeleteTransaction().
SetTopicID(topicID).
SetNodeAccountIDs([]hedera.AccountID{transactionResponse.NodeID}).
Expand All @@ -119,8 +114,4 @@ func main() {
println(err.Error(), ": error getting receipt for topic deletion")
return
}

if wait {
panic("Message was not received within 30 seconds")
}
}

0 comments on commit 3afb659

Please sign in to comment.