github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/examples/v2/trade-feed/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"log"
     6  	_ "net/http/pprof"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/trade"
     9  	"github.com/bitfinexcom/bitfinex-api-go/v2/websocket"
    10  )
    11  
    12  func main() {
    13  	client := websocket.New()
    14  	err := client.Connect()
    15  	if err != nil {
    16  		log.Printf("could not connect: %s", err.Error())
    17  		return
    18  	}
    19  
    20  	for obj := range client.Listen() {
    21  		switch obj.(type) {
    22  		case error:
    23  			log.Printf("channel closed: %s", obj)
    24  			return
    25  		case *trade.Trade:
    26  			log.Printf("New trade: %+v\n", obj)
    27  		case *websocket.InfoEvent:
    28  			// Info event confirms connection to the bfx websocket
    29  			log.Printf("Subscribing to tBTCUSD")
    30  			_, err := client.SubscribeTrades(context.Background(), "tBTCUSD")
    31  			if err != nil {
    32  				log.Printf("could not subscribe to trades: %s", err.Error())
    33  			}
    34  		default:
    35  			log.Printf("MSG RECV: %#v", obj)
    36  		}
    37  	}
    38  }