Skip to main content

StreamBboBook gRPC Method

Loading...

Updated on
Jun 10, 2026

StreamBboBook gRPC Method

Please note that this method is metered based on data consumption at 0.0165 MB = 10 API credits.

Parameters

coins
repeated string
Loading...

Returns

stream
stream<BboBookUpdate>
Loading...
coin
string
Loading...
time
uint64
Loading...
block_number
uint64
Loading...
bid
L2Level
Loading...
ask
L2Level
Loading...
Request
1
// StreamBboBook Example - Stream top-of-book best bid/ask changes via gRPC
2
package main
3
4
import (
5
"context"
6
"flag"
7
"fmt"
8
"io"
9
"log"
10
"math"
11
"strings"
12
"time"
13
14
"google.golang.org/grpc"
15
"google.golang.org/grpc/codes"
16
"google.golang.org/grpc/credentials"
17
"google.golang.org/grpc/metadata"
18
"google.golang.org/grpc/status"
19
20
pb "hyperliquid-orderbook-example/proto"
21
)
22
23
const (
24
grpcEndpoint = "your-endpoint.hype-mainnet.quiknode.pro:10000"
25
authToken = "your-auth-token"
26
maxRetries = 10
27
baseDelay = 2 * time.Second
28
)
29
30
func streamBboBook(coins []string) error {
31
fmt.Println(strings.Repeat("=", 60))
32
fmt.Printf("Streaming BBO Book for %s\n", strings.Join(coins, ", "))
33
fmt.Println("Auto-reconnect: true")
34
fmt.Println(strings.Repeat("=", 60) + "\n")
35
36
retryCount := 0
37
38
for retryCount < maxRetries {
39
creds := credentials.NewClientTLSFromCert(nil, "")
40
conn, err := grpc.Dial(grpcEndpoint,
41
grpc.WithTransportCredentials(creds),
42
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
43
if err != nil {
44
return fmt.Errorf("failed to connect: %w", err)
45
}
46
47
client := pb.NewOrderBookStreamingClient(conn)
48
ctx := metadata.AppendToOutgoingContext(context.Background(), "x-token", authToken)
49
50
request := &pb.BboBookRequest{
51
Coins: coins,
52
}
53
54
if retryCount > 0 {
55
fmt.Printf("\nšŸ”„ Reconnecting (attempt %d/%d)...\n", retryCount+1, maxRetries)
56
} else {
57
fmt.Printf("Connecting to %s...\n", grpcEndpoint)
58
}
59
60
stream, err := client.StreamBboBook(ctx, request)
61
if err != nil {
62
conn.Close()
63
return fmt.Errorf("failed to start stream: %w", err)
64
}
65
66
msgCount := 0
67
shouldRetry := false
68
69
for {
70
update, err := stream.Recv()
71
if err == io.EOF {
72
break
73
}
74
if err != nil {
75
st, ok := status.FromError(err)
76
if ok && st.Code() == codes.DataLoss {
77
fmt.Printf("\nāš ļø Server reinitialized: %s\n", st.Message())
78
retryCount++
79
if retryCount < maxRetries {
80
delay := baseDelay * time.Duration(math.Pow(2, float64(retryCount-1)))
81
fmt.Printf("ā³ Waiting %v before reconnecting...\n", delay)
82
time.Sleep(delay)
83
shouldRetry = true
84
break
85
} else {
86
fmt.Printf("\nāŒ Max retries (%d) reached. Giving up.\n", maxRetries)
87
conn.Close()
88
return nil
89
}
90
}
91
conn.Close()
92
return fmt.Errorf("stream error: %w", err)
93
}
94
95
msgCount++
96
if msgCount == 1 {
97
fmt.Println("āœ“ First BBO update received!\n")
98
retryCount = 0 // Reset on success
99
}
100
101
// Display BBO update
102
fmt.Println("\n" + strings.Repeat("─", 60))
103
fmt.Printf("Block: %d | Time: %d | Coin: %s\n", update.BlockNumber, update.Time, update.Coin)
104
fmt.Println(strings.Repeat("─", 60))
105
106
// Display best ask
107
if update.Ask != nil {
108
fmt.Printf(" BEST ASK: %12s | %12s | (%d orders)\n", update.Ask.Px, update.Ask.Sz, update.Ask.N)
109
} else {
110
fmt.Println(" BEST ASK: (none)")
111
}
112
113
// Display best bid
114
if update.Bid != nil {
115
fmt.Printf(" BEST BID: %12s | %12s | (%d orders)\n", update.Bid.Px, update.Bid.Sz, update.Bid.N)
116
} else {
117
fmt.Println(" BEST BID: (none)")
118
}
119
120
// Display spread
121
if update.Bid != nil && update.Ask != nil {
122
fmt.Printf(" SPREAD: (best bid: %s, best ask: %s)\n", update.Bid.Px, update.Ask.Px)
123
}
124
125
fmt.Printf("\n Messages received: %d\n", msgCount)
126
}
127
128
conn.Close()
129
130
if !shouldRetry {
131
break
132
}
133
}
134
135
return nil
136
}
137
138
func main() {
139
coinsFlag := flag.String("coins", "BTC", "Comma-separated coin symbols to stream (e.g., BTC,ETH)")
140
141
flag.Parse()
142
143
coins := strings.Split(*coinsFlag, ",")
144
145
fmt.Println("\n" + strings.Repeat("=", 60))
146
fmt.Println("Hyperliquid StreamBboBook Example")
147
fmt.Printf("Endpoint: %s\n", grpcEndpoint)
148
fmt.Println(strings.Repeat("=", 60))
149
150
if err := streamBboBook(coins); err != nil {
151
log.Fatal(err)
152
}
153
}
154
1
// StreamBboBook Example - Stream top-of-book best bid/ask changes via gRPC
2
package main
3
4
import (
5
"context"
6
"flag"
7
"fmt"
8
"io"
9
"log"
10
"math"
11
"strings"
12
"time"
13
14
"google.golang.org/grpc"
15
"google.golang.org/grpc/codes"
16
"google.golang.org/grpc/credentials"
17
"google.golang.org/grpc/metadata"
18
"google.golang.org/grpc/status"
19
20
pb "hyperliquid-orderbook-example/proto"
21
)
22
23
const (
24
grpcEndpoint = "your-endpoint.hype-mainnet.quiknode.pro:10000"
25
authToken = "your-auth-token"
26
maxRetries = 10
27
baseDelay = 2 * time.Second
28
)
29
30
func streamBboBook(coins []string) error {
31
fmt.Println(strings.Repeat("=", 60))
32
fmt.Printf("Streaming BBO Book for %s\n", strings.Join(coins, ", "))
33
fmt.Println("Auto-reconnect: true")
34
fmt.Println(strings.Repeat("=", 60) + "\n")
35
36
retryCount := 0
37
38
for retryCount < maxRetries {
39
creds := credentials.NewClientTLSFromCert(nil, "")
40
conn, err := grpc.Dial(grpcEndpoint,
41
grpc.WithTransportCredentials(creds),
42
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(100*1024*1024)))
43
if err != nil {
44
return fmt.Errorf("failed to connect: %w", err)
45
}
46
47
client := pb.NewOrderBookStreamingClient(conn)
48
ctx := metadata.AppendToOutgoingContext(context.Background(), "x-token", authToken)
49
50
request := &pb.BboBookRequest{
51
Coins: coins,
52
}
53
54
if retryCount > 0 {
55
fmt.Printf("\nšŸ”„ Reconnecting (attempt %d/%d)...\n", retryCount+1, maxRetries)
56
} else {
57
fmt.Printf("Connecting to %s...\n", grpcEndpoint)
58
}
59
60
stream, err := client.StreamBboBook(ctx, request)
61
if err != nil {
62
conn.Close()
63
return fmt.Errorf("failed to start stream: %w", err)
64
}
65
66
msgCount := 0
67
shouldRetry := false
68
69
for {
70
update, err := stream.Recv()
71
if err == io.EOF {
72
break
73
}
74
if err != nil {
75
st, ok := status.FromError(err)
76
if ok && st.Code() == codes.DataLoss {
77
fmt.Printf("\nāš ļø Server reinitialized: %s\n", st.Message())
78
retryCount++
79
if retryCount < maxRetries {
80
delay := baseDelay * time.Duration(math.Pow(2, float64(retryCount-1)))
81
fmt.Printf("ā³ Waiting %v before reconnecting...\n", delay)
82
time.Sleep(delay)
83
shouldRetry = true
84
break
85
} else {
86
fmt.Printf("\nāŒ Max retries (%d) reached. Giving up.\n", maxRetries)
87
conn.Close()
88
return nil
89
}
90
}
91
conn.Close()
92
return fmt.Errorf("stream error: %w", err)
93
}
94
95
msgCount++
96
if msgCount == 1 {
97
fmt.Println("āœ“ First BBO update received!\n")
98
retryCount = 0 // Reset on success
99
}
100
101
// Display BBO update
102
fmt.Println("\n" + strings.Repeat("─", 60))
103
fmt.Printf("Block: %d | Time: %d | Coin: %s\n", update.BlockNumber, update.Time, update.Coin)
104
fmt.Println(strings.Repeat("─", 60))
105
106
// Display best ask
107
if update.Ask != nil {
108
fmt.Printf(" BEST ASK: %12s | %12s | (%d orders)\n", update.Ask.Px, update.Ask.Sz, update.Ask.N)
109
} else {
110
fmt.Println(" BEST ASK: (none)")
111
}
112
113
// Display best bid
114
if update.Bid != nil {
115
fmt.Printf(" BEST BID: %12s | %12s | (%d orders)\n", update.Bid.Px, update.Bid.Sz, update.Bid.N)
116
} else {
117
fmt.Println(" BEST BID: (none)")
118
}
119
120
// Display spread
121
if update.Bid != nil && update.Ask != nil {
122
fmt.Printf(" SPREAD: (best bid: %s, best ask: %s)\n", update.Bid.Px, update.Ask.Px)
123
}
124
125
fmt.Printf("\n Messages received: %d\n", msgCount)
126
}
127
128
conn.Close()
129
130
if !shouldRetry {
131
break
132
}
133
}
134
135
return nil
136
}
137
138
func main() {
139
coinsFlag := flag.String("coins", "BTC", "Comma-separated coin symbols to stream (e.g., BTC,ETH)")
140
141
flag.Parse()
142
143
coins := strings.Split(*coinsFlag, ",")
144
145
fmt.Println("\n" + strings.Repeat("=", 60))
146
fmt.Println("Hyperliquid StreamBboBook Example")
147
fmt.Printf("Endpoint: %s\n", grpcEndpoint)
148
fmt.Println(strings.Repeat("=", 60))
149
150
if err := streamBboBook(coins); err != nil {
151
log.Fatal(err)
152
}
153
}
154
Don't have an account yet?
Create your Quicknode endpoint in seconds and start building
Get started for free