KirillBelov/WebSocketJLink

(1.0.0) current version: 1.0.1 »

Web socket connection client

Contributed by: Kirill Belov

This package allows you to connect to remote servers that operate using the WS or WSS protocol. Once connected, you can send messages to the remote server and receive messages.

Installation Instructions

To install this paclet in your Wolfram Language environment, evaluate this code:
PacletInstall["KirillBelov/WebSocketJLink"]

Examples

Basic Examples (5) 

Let's create a connection:

In[1]:=
connection = WebSocketConnect[
  "wss://stream.binance.com:9443/ws/btcusdt@miniTicker"]
Out[1]=

Let's see what came after a few seconds:

In[2]:=
connection["Data"]
Out[2]=

You can retrieve the latest received data:

In[3]:=
ImportString[connection["Data"]["Part", -1], "RawJSON"]
Out[3]=

Let's send a message and subscribe to another stream:

In[4]:=
frame = ExportString[<|"method" -> "SUBSCRIBE", "params" -> {"btcusdt@depth"}, "id" -> 1|>, "RawJSON"]
Out[4]=
In[5]:=
WebSocketSend[connection, frame]
Out[5]=

After that we will be able to receive data of another type:

In[6]:=
Short@
 SelectFirst[#e == "depthUpdate" &]@
  Select[KeyExistsQ[#, "e"] &]@
   Map[ImportString[#, "RawJSON"] &] @ connection["Data"]["Elements"]
Out[6]=

Let's close the connection

In[7]:=
WebSocketClose[connection]
Out[7]=

Scope (10) 

Custom serializer (3) 

By default you can send to the server frames as a string, but you can use custom serialiser for frames:

In[8]:=
connection = WebSocketConnect[
  "wss://stream.binance.com:9443/ws/btcusdt@miniTicker"]
Out[8]=
In[9]:=
miniTickerSerialize[pair_String] := ExportString[<|"method" -> "SUBSCRIBE", "params" -> {pair <> "@miniTicker"}, "id" -> RandomInteger[{1, 1000}]|>, "RawJSON"]

Use this serializer

In[10]:=
WebSocketSend[connection, "ethusdt", "Serializer" -> miniTickerSerialize]
Out[10]=

Extract data for ETHUSDT

In[11]:=
Short[ethusdtTicker =
  Query[All, {"E"/*(#/1000 &)/*FromUnixTime, "c"/*ToExpression}]@
   Select[KeyExistsQ[#, "s"] && #[["s"]] == "ETHUSDT" &]@
    Map[ImportString[#, "RawJSON"] &]@
     connection["Data"]["Elements"]]
Out[11]=

And plot the price of the coin:

In[12]:=
DateListPlot[ethusdtTicker, PlotTheme -> "Marketing", FrameLabel -> {"time", "ETH price in USDT"}]
Out[12]=

Close the connection:

In[13]:=
WebSocketClose[connection]
Out[13]=

Custom deserializer (2) 

Also you can use custom deserializer:

In[14]:=
miniTickerDeserialize[miniTicker_String] :=
 Query[<|"Time" -> "E"/*(#/1000 &)/*FromUnixTime, "Price" -> "c"/*ToExpression|>]@
  ImportString[miniTicker, "RawJSON"]
In[15]:=
connection = WebSocketConnect[
  "wss://stream.binance.com:9443/ws/btcusdt@miniTicker", "Deserializer" -> miniTickerDeserialize
  ]
Out[15]=

Now the saved data is immediately in the form of an expression and not a string

In[16]:=
btcusdtMiniTicker = Values@connection["Data"]["Elements"];

Plot it:

In[17]:=
DateListPlot[btcusdtMiniTicker, PlotTheme -> "Marketing", FrameLabel -> {"time", "BTC price in USDT"}]
Out[17]=

Close connection

In[18]:=
WebSocketClose[connection]
Out[18]=

Event handler (5) 

You can add custom event handler that will be process all received messages

First define a deserializer:

In[19]:=
klineDeserialize[message_String] := Module[{k = ImportString[message, "RawJSON"]}, <|
   "s" -> k["s"], "t" -> FromUnixTime[k["k", "t"]/1000], "o" -> ToExpression[k["k", "o"]], "h" -> ToExpression[k["k", "h"]], "l" -> ToExpression[k["k", "l"]], "c" -> ToExpression[k["k", "c"]], "v" -> ToExpression[k["k", "v"]]
   |>
  ]

Let's create a separate variable to store the results of processing:

In[20]:=
klines = <||>;

And the handler itself, which will not save unnecessary data on candles. After all, candles are completed once in a certain period, and the last candle is updated every few seconds:

In[21]:=
klineEventHandler[kline_] := (
  Print[kline];
  If[! KeyExistsQ[klines, kline["s"]], klines[kline["s"]] = <||>]; klines[kline["s"], kline["t"] ] = {kline["t"], Values[kline[[{"o", "h", "l", "c", "v"}]]]}
  )

Create a connection:

In[22]:=
connection = WebSocketConnect["wss://stream.binance.com:9443/ws/btcusdt@kline_3m", "Deserializer" -> klineDeserialize, "EventHandler" -> klineEventHandler
  ]
Out[22]=

After several minutes you can plot the graphic with BTCUSDT candlestick:

In[23]:=
TradingChart[Values@klines[["BTCUSDT", All]], PlotTheme -> "Detailed"]
Out[23]=

Close connection

In[24]:=
WebSocketClose[connection]
Out[24]=

Publisher

Kirill Belov

Disclosures

Compatibility

Wolfram Language Version 13.0

Version History

  • 1.0.1 – 06 November 2022
  • 1.0.0 – 02 November 2022

License Information

MIT License

Paclet Source

Source Metadata