How to subscribe to the Binance market stream using Julia and WebSockets.jl

Viewed 409

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?

0 Answers
Related