# Using sockets to connect to a trading platform

I'm trying to use Binance's API. The following works:

URLExecute[ <|"Scheme" -> "http", "Domain" -> "api.binance.com",
"Method" -> "GET", "Path" -> "/api/v1/time"|>]
(* {"serverTime" -> 1519522573487} *)


but I would like to use the socket instead. I tried

socket = SocketConnect[{"api.binance.com", 443}]
WriteString[socket, "GET /api/v1/time"]


but the last command never stops (even without the WriteString). I also tried replacing the first line with socket = SocketConnect["stream.binance.com:9443"], see binance doc, with the same result.

What is wrong and how can I connect to the socket to return, e.g., the server time or last orders?

Related: 26875 but does not use Mathematica's latest features and 165297.

socket = SocketConnect[{"api.binance.com", 443}]
WriteString[socket, "GET /api/v1/time \n"]


note the "\n" message returns a bytearray. Without the "\n" it hangs. I am not familiar with the binance API but just generally sometimes one or more carriage returns are necessary in HTTP headers.

ByteArrayToString[%]


Returns a 400 error page. I am assuming you are looking for a timestamp.

• At least I get an error now... I also get a 400 Bad Request error with WriteString[socket, "!ticker@arr\n"] (even without \n) but the string comes from the doc. And yes, I am looking for a timestamp or anything that shows that the connection with the socket is working, such as any live data from the website. – anderstood Feb 27 '18 at 18:17
• I would say an error shows that the connection or parts of it is working, The remainder maybe how to send proper header and payload to get back the proper response. I was only trying to get you past the hanging. The link you provide does not help much as I see no info on the time part of the api. – Hans Feb 27 '18 at 18:36
• I see. There is another doc which is not dedicated to sockets, that mentions time in section "check server time". – anderstood Feb 27 '18 at 18:38
• I am not sure that SocketConnect is setup to do secure communications or the handshake that is required for https. – Hans Feb 28 '18 at 5:16