Reading and writing to a websocket

What is the accepted way to read and write to a websocket?

The only option that I’ve found is this [1]. The problem with it is not only that it’s obviously “outside” of the HTTP.WebSockets API, but also that does so in a way that was explicitly forbidden by the API designers (seen quinnj’s comment above NAS’s comment), which leads me to believe that solution my actually be broken in some subtle way.

What I thought could be a solution would be something like (schematically)

c = Channel()
HTTP.WebSockets.open(url) do ws
    for msg in ws
        myhandler(msg)  # handle the message we just received
        if isready(c)  # check if channel has something in it..
            s = take!(c)  # .. if so take it ...
            HTTP.WebSockets.send(ws, s)  # .. and send it to the websocket
        end
    end
end

and you would send messages by putting them into the channel: put!(c, mymessage_str)

The problem that this has however is that the websocket iterator is blocking. So once there’s nothing to receive, we block, and we never again check if there’s something in the channel to send.

In my mind the solution would be a non-blocking function that checks if there’s something in the websocket to be recieved before trying to iterate it. Something like

c = Channel()
HTTP.WebSockets.open(url) do ws
    while true
        if has_stuff_to_be_received(ws)  # non-blocking, does this exist?
           msg, _ = iterate(ws)
           myhandler(msg)
        end
        if isopen(c)
            s = take!(c)
            HTTP.WebSockets.send(ws, s)
        end
    end
end

Am I missing something? Is there an idiomatic, non-hacky way to do this?

[1] HTTP.jl Websockets- help getting started - #4 by NAS

Bump