JuliaWeb / JuliaWeb/WebSockets.jl

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

Open
#185 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
161
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Hello,

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

```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

```julia
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.