niv / websocket.nim

websockets for nim
http://niv.github.io/websocket.nim/docs/0.1.1/websocket.html
Other
102 stars 25 forks source link

Client doesn't keep receiving data depending on how it is created #43

Closed Toshiyuki-Tega closed 6 years ago

Toshiyuki-Tega commented 6 years ago

I have written a very simple program which subscribe to BitMEX exchange trade data.

import websocket, asyncnet, asyncdispatch

# This doesn't work (method A); only initial welcome message is received
let ws = waitFor newAsyncWebsocketClient("wss://www.bitmex.com:443/realtime?subscribe=trade:XBTUSD")

# This works (method B).
# let ws = waitFor newAsyncWebsocketClient("www.bitmex.com", Port 443, "/realtime?subscribe=trade:XBTUSD", ssl=true)

proc reader() {.async.} =
    while true:
        let read = await ws.readData()
        echo "read: ", read

asyncCheck reader()
runForever()

When the client object is created by providing a full URI like method A above, only the initial welcome message is received.

read: (opcode: Text, data: "{\"info\":\"Welcome to the BitMEX Realtime API.\",\"version\":\"2018-09-07T19:37:21.000Z\",\"timestamp\":\"2018-09-12T16:52:07.992Z\",\"docs\":\"https://www.bitmex.com/app/wsAPI\",\"limit\":{\"remaining\":38}}")

However, if it is created like method B by providing host name, port and the path separately, it does receive subsequent trade data successfully.

read: (opcode: Text, data: "{\"info\":\"Welcome to the BitMEX Realtime API.\",\"version\":\"2018-09-07T19:37:21.000Z\",\"timestamp\":\"2018-09-12T17:03:15.805Z\",\"docs\":\"https://www.bitmex.com/app/wsAPI\",\"limit\":{\"remaining\":39}}")
read: (opcode: Text, data: "{\"success\":true,\"subscribe\":\"trade:XBTUSD\",\"request\":{\"op\":\"subscribe\",\"args\":\"trade:XBTUSD\"}}")
read: (opcode: Text, data: "{\"table\":\"trade\",\"action\":\"partial\",\"keys\":[],\"types\":{\"timestamp\":\"timestamp\",\"symbol\":\"symbol\",\"side\":\"symbol\",\"size\":\"long\",\"price\":\"float\",\"tickDirection\":\"symbol\",\"trdMatchID\":\"guid\",\"grossValue\":\"long\",\"homeNotional\":\"float\",\"foreignNotional\":\"float\"},\"foreignKeys\":{\"symbol\":\"instrument\",\"side\":\"side\"},\"attributes\":{\"timestamp\":\"sorted\",\"symbol\":\"grouped\"},\"filter\":{\"symbol\":\"XBTUSD\"},\"data\":[{\"timestamp\":\"2018-09-12T17:03:13.014Z\",\"symbol\":\"XBTUSD\",\"side\":\"Buy\",\"size\":200,\"price\":6260.5,\"tickDirection\":\"ZeroPlusTick\",\"trdMatchID\":\"0d0a4c64-17e3-4738-f243-b33dffa52a1e\",\"grossValue\":3194600,\"homeNotional\":0.031946,\"foreignNotional\":200}]}")
r

What's going wrong with the client when created like method A?

dom96 commented 6 years ago

Maybe it's parsing the URL incorrectly, sprinkle some echo into the code :)

metagn commented 6 years ago

Apparently we ignore the query of the URI when passing the path

metagn commented 6 years ago

Should work now with https://github.com/niv/websocket.nim/commit/6d0836fc10b3ba8c26f3485274895715dfd014c8, try updating to 0.3.3 and see if it works

Toshiyuki-Tega commented 6 years ago

Ah, so it wasn't receiving trade data at all because the query part was removed in method A.

Confirmed that both methods now receive trade data as expected. Thanks for prompt fix!