github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v1/ws-book/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/bitfinexcom/bitfinex-api-go/v1"
     8  )
     9  
    10  func main() {
    11  	c := bitfinex.NewClient()
    12  
    13  	// in case your proxy is using a non valid certificate set to TRUE
    14  	c.WebSocketTLSSkipVerify = false
    15  
    16  	err := c.WebSocket.Connect()
    17  	if err != nil {
    18  		log.Fatal("Error connecting to web socket : ", err)
    19  	}
    20  	defer c.WebSocket.Close()
    21  
    22  	book_btcusd_chan := make(chan []float64)
    23  	book_ltcusd_chan := make(chan []float64)
    24  	trades_chan := make(chan []float64)
    25  	ticker_chan := make(chan []float64)
    26  
    27  	c.WebSocket.AddSubscribe(bitfinex.ChanBook, bitfinex.BTCUSD, book_btcusd_chan)
    28  	c.WebSocket.AddSubscribe(bitfinex.ChanBook, bitfinex.LTCUSD, book_ltcusd_chan)
    29  	c.WebSocket.AddSubscribe(bitfinex.ChanTrade, bitfinex.BTCUSD, trades_chan)
    30  	c.WebSocket.AddSubscribe(bitfinex.ChanTicker, bitfinex.BTCUSD, ticker_chan)
    31  
    32  	go listen(book_btcusd_chan, "BOOK BTCUSD:")
    33  	go listen(book_ltcusd_chan, "BOOK LTCUSD:")
    34  	go listen(trades_chan, "TRADES BTCUSD:")
    35  	go listen(ticker_chan, "TICKER BTCUSD:")
    36  
    37  	err = c.WebSocket.Subscribe()
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  }
    42  
    43  func listen(in chan []float64, message string) {
    44  	for {
    45  		msg := <-in
    46  		fmt.Println(message, msg)
    47  	}
    48  }