Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subscribing to a websocket using gorilla/websocket

Tags:

websocket

go

I want to use some websocket streams of the Binance API. I am posting this here because I think this isn't an issue with the API, but rather my understanding of websockets in general.

I try to subscribe to a stream as the official gorilla/websocket example describes:

conn, res, err := websocket.DefaultDialer.Dial("wss://stream.binance.com/ws/BTCUSD@markPrice", nil)

fmt.Println(conn)
fmt.Println(res)
fmt.Println(err)

for {
    _, message, readErr := conn.ReadMessage()
    if readErr != nil {
        fmt.Println(readErr)
        return
    }
    fmt.Println(message)
}

The connection is created without errors, but after that, no message gets read.

I think the problem is that the API demands me to subscribe to a stream like so:

{
    "method": "SUBSCRIBE",
    "params": [
        "btcusdt@aggTrade",
        "btcusdt@depth"
    ],
    "id": 1
}

I am aware that a websocket connection starts out with one HTTP request where, as far as I understood, I send this data. But where and when am I supposed to do so? Dial doesn't give the option to send anything apart from HTTP headers.

UPDATE:

I managed to send the right request and get the right response as documented here by using Conn.WriteJSON:

type request struct {
    Method string    `json:"method"`
    Params [1]string `json:"params"`
    ID     int       `json:"id"`
}

markPriceReq := request{"SUBSCRIBE", [1]string{"btcusdt@markPrice"}, 1}
conn.WriteJSON(markPriceReq)

However, after the initial response no data gets read anymore. Where do I get the actual mark price data from?

like image 828
Jan Berndt Avatar asked Sep 16 '25 06:09

Jan Berndt


1 Answers

There is an issue with your connection url.

All symbols for stream are lowercase and the ws base endpoint in the document link you posted is wss://stream.binancefuture.com (Perpetual/Perpetual Testnet ws endpoint) but you are using wss://stream.binance.com (Margin/Spot/Savings/Mining ws endpoint).

Mark price stream is only available for Perpetual/Perpetual Testnet ws endpoint. I use raw stream url: wss://stream.binancefuture.com/ws/btcusdt@markPrice without any issues

This request is for live subscribing/unsubscribing, you only use this when already connected to binance ws and want to subscribe to more streams or unsubscribe to a stream.

{
    "method": "SUBSCRIBE",
    "params": [
        "btcusdt@aggTrade",
        "btcusdt@depth"
    ],
    "id": 1
}

You don't have to use live subscribing/unsubscribing, just use raw url like above is fine:

wss://stream.binancefuture.com/ws/<streamName> (only one stream) wss://stream.binancefuture.com/stream?streams=<streamName1>/<streamName2>/<streamName3> (combined streams)

By using raw url, you are accessed to <streamName> (you can also send a request to use live subscribing/unsubscribing after that)

like image 174
Son Huynh Avatar answered Sep 17 '25 22:09

Son Huynh