Skip to content

Commit 40daeb7

Browse files
committed
fix: Heartbeat message for bybit
1 parent b316fac commit 40daeb7

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

pkg/connector/bybit/connector.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log/slog"
88
"strings"
9+
"time"
910

1011
"github.com/google/uuid"
1112
"github.com/gorilla/websocket"
@@ -42,19 +43,23 @@ func NewConnector(ctx context.Context, wsUrl string, pairs []string) (connector.
4243

4344
req := SubscriptionMessage{
4445
Op: "subscribe",
45-
ReqId: uuid.New(),
46+
ReqId: uuid.New().String(),
4647
Args: args,
4748
}
4849

4950
if err := ws.WriteJSON(req); err != nil {
5051
return nil, err
5152
}
5253

53-
return &Connector{
54+
connector := &Connector{
5455
ctx: ctx,
5556
pairs: pairs,
5657
ws: ws,
57-
}, nil
58+
}
59+
60+
// We send heart beat every 10 seconds
61+
connector.sendHeartbeat()
62+
return connector, nil
5863
}
5964

6065
func (c *Connector) Close() error {
@@ -107,3 +112,20 @@ func (c *Connector) Name() string {
107112
func (c *Connector) Tickers() []string {
108113
return c.pairs
109114
}
115+
116+
func (c *Connector) sendHeartbeat() {
117+
req := SubscriptionMessage{
118+
Op: "ping",
119+
ReqId: "100001",
120+
}
121+
122+
// We send heart beat every 10 seconds
123+
go func() {
124+
for {
125+
time.Sleep(10 * time.Second)
126+
if err := c.ws.WriteJSON(req); err != nil {
127+
slog.Warn("error sending heartbeat", "error", err, "exchange", Name)
128+
}
129+
}
130+
}()
131+
}

pkg/connector/bybit/types.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package bybit
22

33
import (
44
"math/big"
5-
6-
"github.com/google/uuid"
75
)
86

97
type SubscriptionMessage struct {
10-
ReqId uuid.UUID `json:"req_id"`
11-
Op string `json:"op"`
12-
Args []string `json:"args"`
8+
ReqId string `json:"req_id"`
9+
Op string `json:"op"`
10+
Args []string `json:"args"`
1311
}
1412

1513
type TickerResponse struct {

0 commit comments

Comments
 (0)