Monitoring for new blocks
A Tezos node can notify applications when new blocks are attached to the chain. The Tezos RPC calls this monitor and technically it's a long-poll implementation. Here's how to use this feature in TzGo:
import "github.com/trilitech/tzgo/rpc"
// init SDK client
c, _ := rpc.NewClient("https://rpc.tzpro.io", nil)
// create block header monitor
mon := rpc.NewBlockHeaderMonitor()
defer mon.Close()
// all SDK functions take a context, here we just use a dummy
ctx := context.TODO()
// register the block monitor with our client
if err := c.MonitorBlockHeader(ctx, mon); err != nil {
log.Fatalln(err)
}
// wait for new block headers
for {
head, err := mon.Recv(ctx)
if err != nil {
log.Fatalln(err)
}
// do smth with the block header
fmt.Printf("New block %s\n", head.Hash)
}