github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v2/ws-book/main.go (about) 1 package main 2 3 import ( 4 "context" 5 "log" 6 "time" 7 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/common" 9 "github.com/bitfinexcom/bitfinex-api-go/v2" 10 "github.com/bitfinexcom/bitfinex-api-go/v2/websocket" 11 ) 12 13 func main() { 14 p := websocket.NewDefaultParameters() 15 // Enable orderbook checksum verification 16 p.ManageOrderbook = true 17 c := websocket.NewWithParams(p) 18 19 err := c.Connect() 20 if err != nil { 21 log.Fatal("Error connecting to web socket : ", err) 22 } 23 24 // subscribe to BTCUSD book 25 ctx, cxl2 := context.WithTimeout(context.Background(), time.Second*5) 26 defer cxl2() 27 _, err = c.SubscribeBook(ctx, common.TradingPrefix+bitfinex.BTCUSD, common.Precision0, common.FrequencyRealtime, 25) 28 if err != nil { 29 log.Fatal(err) 30 } 31 32 // subscribe to BTCUSD trades 33 ctx, cxl3 := context.WithTimeout(context.Background(), time.Second*5) 34 defer cxl3() 35 _, err = c.SubscribeTrades(ctx, common.TradingPrefix+bitfinex.BTCUSD) 36 if err != nil { 37 log.Fatal(err) 38 } 39 40 for obj := range c.Listen() { 41 switch obj.(type) { 42 case error: 43 log.Printf("channel closed: %+v\n", obj) 44 default: 45 } 46 log.Printf("MSG RECV: %+v\n", obj) 47 48 // Load the latest orderbook 49 ob, _ := c.GetOrderbook(common.TradingPrefix + bitfinex.BTCUSD) 50 if ob != nil { 51 log.Printf("Orderbook asks: %v", ob.Asks()) 52 log.Printf("Orderbook bids: %v", ob.Bids()) 53 } 54 } 55 }