Connecting to an API via WebSocket

Hi everyone, sorry if this is a really noob question but I’m I’m trying to translate some instructions in NodeJS to connect to an API into a Julia interface. I’m not a web developer and the documentation in HTTP.jl is miserable, so I have no choice but to ask for some help.

I’m trying to translate the instructions here: Newton API Docs

The NodeJS code looks like this

const io = require("");

const socket = io.connect(
    transports: ["websocket"],
    reconnection: false,


socket.on("initial", (data) => {
  //Initial logic

socket.on("update", (data) => {
  //Update Logic

An acceptable choice for the parameters would be symbol="BTC_USD" and candle="1m".
I tried starting with the WebSocket example in HTTP.jl but I’m pretty sure the example is far too minimalistic to replicate the code above.

Can anyone give some pointers? Thanks!

JavaScript by default handles communication with events. HTTP.jl doesn’t expose an event based API though, it presents a WebSocket as a regular Socket (like UDP or TCP). You’re going to have to parse the response you get and decide based on the message which method to call.

I’d start with the code from HTTP.jl and investigate what the remote server is actually sending to you - my best bet is it’ll be some kind of JSON which you should be able to unpack and work with.

HTTP.jl handles websockets via openraw. the websocket example in http.jl should work fine. It seems is using AWS style auth (mash all this stuff together, take the sha256 hash of it, submit it as an authentication for the next 5m).

Most likely, is sending back JSON which JSON.jl should handle.

Thanks for the replies @Sukera and @sampope. Here’s the code I’ve been trying to run so far

using HTTP

symbol, candle = "BTC_CAD", "1m"
channel = "v1/chart-data"

url = "$(channel)?symbol=$(symbol)&candle=$(candle)" do ws
  write(ws, "subscribe")
  x = readavailable(ws)
  @show x
# transports: ["websocket"], reconnection: false,

All I get is the following error

ERROR: LoadError: HTTP.ExceptionRequest.StatusError(502, "GET", "/v1/chart-data?symbol=BTC_CAD&candle=1m", HTTP.Messages.Response:
HTTP/1.1 502 Bad Gateway
Server: awselb/2.0
Date: Wed, 04 Aug 2021 05:22:24 GMT
Content-Type: text/html
Content-Length: 122
Connection: keep-alive


I’m not sure how to replicate the .emit() and .on() functionality using the HTTP.jl example. Any idea how?

First and foremost, the server is returning an error and no actual data. I’m not familiar with the API you’re trying to target, but there’s bound to be some documentation about why/when a 502 HTTP error is returned, so you’ll have to figure out why that happens before you can continue trying to parse the real response.

1 Like

@Sukera Well the NodeJS example in the documentation works. I think the reason is because the HTTP.jl example connects to the url using a HTTP protocol instead of websocket so it’s being rejected.

@sampope The openraw example is interesting but unfortunately it has been deprecated. The example doesn’t work and the documentation is no longer available in the latest version of HTTP.jl

Hello newton use not exactly the same as websockets. I’m currently working on creating a guile scheme client to newton using Being completely ignorant of Julia it’s possible you can use FFI. on the other hand if Julia has a binding to gobject introspection I could create a glib library in which case guile and julia could both use it. let me know how this works and possibly we could pool resources .