Skip to content

Instantly share code, notes, and snippets.

@blockpane
Last active August 24, 2021 03:36
Show Gist options
  • Save blockpane/87824e7d741debc097e191d19fbc6dd2 to your computer and use it in GitHub Desktop.
Save blockpane/87824e7d741debc097e191d19fbc6dd2 to your computer and use it in GitHub Desktop.
Tendermint subscribe example
package main
import (
"context"
"encoding/json"
"fmt"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"github.com/tendermint/tendermint/types"
"time"
)
func main() {
client, _ := rpchttp.New("tcp://127.0.0.1:26657", "/websocket")
err := client.Start()
if err != nil {
panic(err)
}
defer client.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
query := "tm.event = 'Tx'"
event, err := client.Subscribe(ctx, "test-client", query)
if err != nil {
panic(err)
}
func() {
for e := range event {
tx := e.Data.(types.EventDataTx)
j, _ := json.MarshalIndent(json.RawMessage(tx.TxResult.Result.Log), "", " ")
fmt.Println(string(j))
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment