5

I'm trying for a while now to connect to the Binance market stream using the WebSockets.jl package but I do not get any response. I've already had a look here and found this question, but this results in the same problem as described: the inbox channel is just waiting but gets no response.

The code I've tried is as follows:

    using WebSockets, JSON
    
    uri = "wss://stream.binance.com:9443"
    json_part = "{'method': 'SUBSCRIBE', 'params': ['btcusdt@depth'], 'id': 1}"
    inbox = Channel{String}(10)
    outbox = Channel{String}(10)

    ws_task = @async WebSockets.open(uri) do ws
           inbox_task = @async while !eof(ws)
               put!(inbox, String(read(ws)))
           end
           outbox_task = @async while isopen(ws)
               write(ws, take!(outbox))
           end
       end

    put!(outbox, json_part)
    take!(inbox)

I've never used WebSockets before, so probably I'm just using the wrong endpoint or request formatting. But I can't find a better documentation or useful example.

Can someone please help me get this to work?

1 Answers1

0

The following ought to work:

using WebSockets, JSON
    
uri = "wss://stream.binance.com:9443"
json_part = "{\"method\": \"SUBSCRIBE\", \"params\": [\"btcusdt@depth\"], \"id\": 1}"
inbox = Channel{String}(10)
outbox = Channel{String}(10)

ws_task = @async WebSockets.open(uri) do ws
    inbox_task = @async while !eof(ws)
        put!(inbox, String(read(ws)))
    end
    outbox_task = @async while isopen(ws)
        write(ws, take!(outbox))
    end
end

@async put!(outbox, json_part)
@async take!(inbox)

though I don't have access to a Binance account, so if someone could confirm, that would be great!

Mark
  • 7,785
  • 2
  • 14
  • 34