Skip to content

create_connection, send and recv instead of simply open / avoid do statements #185

@femtotrader

Description

@femtotrader

Hello,

Sorry I'm new to websockets.
I want to convert the following Python code to Julia

from websocket import create_connection
import orjson

ws = create_connection("wss://ws.kraken.com/")
message = {"event":"subscribe", "subscription":{"name":"trade"}, "pair":["XBT/USDT","XBT/USD"]}

ws.send(orjson.dumps(message))

while True:
    message = ws.recv()
    message = orjson.loads(message)
    print(message)
    if isinstance(message, list):
        print(message[2], message[3])

I did using doc

using WebSockets
import JSON


const url = "wss://ws.kraken.com/"

WebSockets.open(url) do ws_client  
    # msg = "{\"event\":\"subscribe\", \"subscription\":{\"name\":\"trade\"}, \"pair\":[\"XBT/USD\",\"XRP/USD\"]}"

    msg = Dict(
        "event" => "subscribe", 
        "subscription" => Dict("name" => "trade"),
        "pair" => ["XBT/USDT", "XBT/USD"]
    )
    msg = JSON.json(msg)

    writeguarded(ws_client, msg)

    while (true)
        data, success = readguarded(ws_client)
        if success
            println(stderr, ws_client, " received:", String(data))
        end
            
    end
end;

but I don't like Julia do statements.

Moreover I would prefer to have separate create_connection, send and recv call like in the Python example but in the Julia example open takes handle function as parameter which is a mix of all this.

How can I achieve that? (ie having a Julia code nearer than the Python code)

Kind regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions