2

using the following code ,

package main

import (
"fmt"
"github.com/gorilla/websocket"
"log"
)

type Message struct {
Id      int         `json:"id"`
Jsonrpc string      `json:"jsonrpc"`
Method  string      `json:"method"`
Params  interface{} `json:"params"`
}

func main() {
// create a new WebSocket connection to the Polkadot node
endpoint := fmt.Sprintf("ws://%s:%d", "localhost", 9944)
conn, _, err := websocket.DefaultDialer.Dial(endpoint, nil)
if err != nil {
    log.Fatal("dial:", err)
}
defer conn.Close()

// subscribe to new block headers
message := Message{
    Id:      1,
    Jsonrpc: "2.0",
    Method:  "system_health",
    Params:  []interface{}{},
}
err = conn.WriteJSON(message)
if err != nil {
    log.Fatal("write:", err)
}

// listen for new peers
for {
    _, message, err := conn.ReadMessage()
    if err != nil {
        log.Fatal("read:", err)
    }
    fmt.Printf("New message: %s\n", message)
    // Do something with the new message
}
}

i get this response only once

{"jsonrpc":"2.0","result{"peers":32,"isSyncing":true,"shouldHavePeers":true},"id":1}

and the the code blocks here : conn.ReadMessage()

however i expected it will stream the changes every time the peers count changes or isSyncing changes for example

{"jsonrpc":"2.0","result{"peers":32,"isSyncing":true,"shouldHavePeers":true},"id":1}
{"jsonrpc":"2.0","result{"peers":33,"isSyncing":true,"shouldHavePeers":true},"id":1}
{"jsonrpc":"2.0","result{"peers":34,"isSyncing":true,"shouldHavePeers":true},"id":1}

and so one.

am i missing something here ?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.