Websocket Reference
Here you will find a guide on establishing a connection to the Blockchain WebSocket client. This guide will walk you through the step-by-step process of connecting and receiving messages.
To connect to the Blockchain WebSocket client, follow these steps.
- 1.Connect to the WebSocket URL.
- 2.Establish a connection to it using a WebSocket library or framework of your choice.
- 3.After successfully connecting, it's important to send an initial message to subscribe to the desired topics or a specific address or hash. This message should contain the necessary information for the server to understand your subscription preferences.
- 4.Once subscribed, you will start receiving messages related to the subscribed topics or a specific address or hash.
ws_client.go
1
package main
2
3
import (
4
"github.com/gorilla/websocket"
5
"log"
6
"net/url"
7
)
8
9
type EventType string
10
11
const (
12
ACCOUNTS EventType = "accounts" // receive all accounts changed or created
13
BLOCKS EventType = "blocks" // receive all blocks
14
TRANSACTION EventType = "transaction" // receive all transactions
15
)
16
17
func main() {
18
u := url.URL{Scheme: "wss", Host: "websocket.mainnet.klever.finance", Path: ""}
19
log.Printf("connecting to %s", u.String())
20
21
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
22
if err != nil {
23
log.Fatal("dial:", err)
24
}
25
defer c.Close()
26
27
done := make(chan struct{})
28
29
go func() {
30
defer close(done)
31
for {
32
_, message, err := c.ReadMessage()
33
if err != nil {
34
log.Println("read:", err)
35
return
36
}
37
38
log.Println(string(message))
39
}
40
}()
41
42
subMessage := struct {
43
Action string `json:"action"`
44
Data struct {
45
Address string `json:"address"`
46
Hash string `json:"hash"`
47
Topics []EventType `json:"topics"`
48
} `json:"data"`
49
}{
50
Action: "subscribe",
51
Data: struct {
52
Topics []EventType `json:"topics"`
53
Address string `json:"address"`
54
Hash string `json:"hash"`
55
}{
56
Topics: []EventType{indexer.ACCOUNTS},
57
Address: "klv1....", // OPTIONAL field to listen a specific address
58
Hash: "hash11.." // // OPTIONAL field to listen a specific tx hash
59
},
60
}
61
// send the first message to subscribe
62
if err := c.WriteJSON(subMessage); err != nil {
63
log.Println("err: ", err)
64
done <- struct{}{}
65
}
66
67
<-done
68
}
ws_client.ts
1
import useWebSocket from 'react-use-websocket'
2
3
export enum WSTopics {
4
accounts = 'accounts', // receive all accounts changed or created
5
blocks = 'blocks', // receive all blocks
6
transaction = 'transaction', // receive all transactions
7
}
8
9
const socketUrl = "wss://websocket.mainnet.klever.finance"
10
11
const { sendJsonMessage, lastJsonMessage } = useWebSocket(socketUrl, {
12
onOpen: () => {
13
const payload = {
14
action: 'subscribe',
15
data: {
16
address: "klv1....", // OPTIONAL field to listen a specific address
17
topics: [WSTopics.blocks],
18
hash: "hash11.." // // OPTIONAL field to listen a specific tx hash
19
},
20
}
21
sendJsonMessage(payload)
22
}
23
})
Topic | Value |
---|---|
BLOCKS | blocks |
TRANSACTIONS | transactions |
ACCOUNTS | accounts |
Last modified 1mo ago