Coinfer Cloud WebSocket
The problem
The old API to get experiment data always returns the whole data, thus having performance problems. So we are going to make some changes. The general approach is to only return the data the user is interested in. The client need to subscribe to some variables. Only after that the server will return the data of these variables. By default no data will be returned.The workflow
- client make WebSocket connection, server accept the connection. client do auth, server validate the auth.
- when auth validation passes, server send out the initial variable names (the names collected until now)
- as the experiment continues, server will continuously collect new variables. When it found some variable names not sent to the client, it sends out the new variable names
- at the same time, the client may subscribe to some variables
- when the server receives a subscription to a variable, it first sends out the initial sample data of this variable. And then the server will remember the subscription and will continuously send out sample data of the subscription when experiment running.
- the client may also unsubscribe to a variable. In this case the server will just forget the subscription, so no sample data about this variable will be send out anymore. If the client wants to subscribe the variable later, the same process in [4,5] will happen.
Message Format
Auth
Send out variable names
the server send out the known variable names after connection established and when new variables are collectedSubscribe
client send out type==“subscribe” when user want to see the data of a variable.Unsubscribe
client send out type==“unsubscribe” when user no longer interested in a variableLog Data
Variable Data
the format used both in initial data and incremental dataExamples
Let’s use a simple python script to demonstrate these protocols.- The script starts by importing necessary modules -
json
, andconnect
fromwebsockets.sync.client
. - Sets up a few variables:
ws_root
: the WebSocket root URLtoken
: the authentication tokenxid
: an experiment identifier.
- Defines several JSON messages:
msg_auth
for authenticationmsg_sub
for subscribing to certain variablesmsg_unsub
for unsubscribing from certain variables.
- The script then opens a WebSocket connection to the server using the
connect
function, with the URL constructed by combiningws_root
andxid
. - After the connection is established, the script sends the authentication message (
msg_auth
) to the server using thesend
function. - It then receives messages from the server using the
recv
function. The first message received should be the log data, and the second message should be the initial variable names. Both messages are printed to the console. - The script then sends the subscription message (
msg_sub
) to the server, to subscribe to certain variables. The server’s response message should contain the data of the subscribed variables, which is then printed to the console. - Finally, the script sends the unsubscription message (
msg_unsub
) to the server, to unsubscribe from certain variables.