What is SocketIO

Socket.IO is a library for real-time communication between client and server. It mainly uses WebSocket but can automatically switch to simpler methods like long-polling if needed. Socket.IO is popular for apps that need quick data exchange, such as chats, online games, or notification systems. It makes working with WebSocket easier and more reliable.

Installation

Standard installation like any other package:
PacletInstall["KirillBelov/SocketIOLink"]
PacletObject
Name: KirillBelov/SocketIOLink
Version: 1.0.4

It is important to note that this package requires a number of dependencies. You can install them all like this, although this is not a necessary step, since the code for installing them is run when the package is imported:
PacletInstall/@{​​"KirillBelov/Objects",​​"KirillBelov/CSockets",​​"KirillBelov/LTP"​​};

Importing

Standard importing:
Get["KirillBelov`SocketIOLink`"]

Connection

So, I spent a long time searching but couldn’t find a good example to show how this library works. The best option I found was the cryptocurrency database from Wolfram Blockchain Lab. It requires signing up and logging in, so it’s not open to everyone, but it’s perfect for demonstrations. I already have an account there, so I can easily show you how the library works. You can find the database documentation here, and if you have questions about the database itself, feel free to contact my friend Jon. Now let’s create a connection.
connection=SocketIOConnect["https://access.ccdb.waexservices.com","HTTPHeaders"->LocalSymbol["MyCreadentialsFromCCDB"]​​]
SocketIOConnection
Self: KirillBelov`SocketIOLink`SocketIOConnection`$15
Endpoint: https://access.ccdb.waexservices.com

To listen for a specific type of event, I need to call a special function directly. The first argument is the connection, the second argument is the event type, and the third argument (optional) is a function. This function will be called automatically with the message object when the event arrives from the server:
In[]:=
eventType="AggregateOHLCV";​​function=Echo;​​SocketIOListen[connection,eventType,function];
Now, after I’ve set up listening for this event, I need to send a message to the server. This message creates my subscription. The server then knows it should send me all new events of type AggregateOHLCV through my connection. You can find more details about event types in the service documentation:
In[]:=
SocketIOEmit[connection,​​"subscribe-to-emitter",​​<|"emitterName"->"AggregateOHLCVEmitter"|>,​​Echo​​];
I pointed out in the third of Echo’s arguments. This is an event handler that is called to confirm that a message has been sent to the server. Echo simply means that the message will be printed:
After subscribing, I need to wait for some time until the event happens on the server. After waiting a bit, the result will appear in the Messages window and will look like this:
Of course, instead of just printing, I can call my own function to do something useful with the data. For example, I can watch the BTC price, and once it crosses a certain value, send a notification to Telegram using a Telegram bot.
But what if I don’t know which event type to expect? There’s another function for this, allowing me to listen to all events from the server. It looks almost the same, but without specifying the event type as the second argument:
In[]:=
SocketIOListenAll[connection,Echo[#,"COMMON HANDLER:"]&];
Now I’ll subscribe to other events, but I won’t specify the event type to listen for. After waiting, I’ll see that my new handler prints out an event that I didn’t explicitly ask to listen for:
In[]:=
SocketIOEmit[connection,"subscribe-to-emitter",​​<|​​"emitterName"->"PricingEmitter",​​"payload"-><|​​"type"->"Ticker",​​"marketId"->1​​|>​​|>,​​Print​​];

Conclusions

This is an initial version of the package, and I’m actively working to improve it. Feel free to ask questions—I’ll be happy to answer them. Also, suggest what else you would like me to add to this package.
​
I have a question for the readers, too: Do you know any services using Socket.IO technology that we could use to test and demonstrate this library?
​
Thank you for your attention!

CITE THIS NOTEBOOK

SocketIOLink - Socket.IO client implementation​
by Kirill Belov​
Wolfram Community, STAFF PICKS, March 14, 2025
​https://community.wolfram.com/groups/-/m/t/3416535