WebSocket API RESTful API
Interface URI: wss://ajubit.com:8080Returns the result format: JSON
WebSocket API
The WebSocket API provides developers with a more real-time data channel. The server can push the changes of the orderbook and the market transaction data to the users of the WebSocket API in real time through the websocket long connection.
Using the WebSocket API can be divided into three simple steps:
- 1. Establish a websocket connection
- 2. Use api token to verify identity
- 3. Receive and process data
Establish a websocket connection
WebSocket is a widely used standard, and each language has its own support library. Take Javascript as an example:
Javascript:
var host = 'wss://ajubit.com:8080';
var socket = new WebSocket(host);
socket.onopen = function() {
console.log("opened");
}
socket.onmessage = function(msg) {
console.log(msg);
}
socket.onclose = function() {
console.log("closed");
}
Use api token to verify identity
Before you can verify your identity, you must have your access/secret key ready. After registering and passing the authentication, just visit the API key page to get your key.
After you establish a WebSocket connection with the server through the previous step, the server will return a challenge message with the following format:
{"challenge":"d45sSFIZZdYzRgwi-zDqA8HFP2MfVoWqXlHX-2LbB_37q9_3pkZ8og"}
The challenge contains a randomly generated string. The client needs to sign the string with the access/secret key, and then send the signature back to the server. The server verifies the signature. If yes, continue to the next step. Otherwise, an error message is returned.
Signature generation is very simple, first connect your access key with the challenge string:
payload = access_key + challenge
Suppose the access key is "abc", the challenge is "def", and the payload is "abcdef". Next, use HMAC-SHA256 and secret key to calculate the signature of the payload:
signature = HMAC-SHA256(secret_key, payload).to_hex
Assuming that secret_key is 'ghi', the result of signing the payload in the above example with HMAC-SHA256 is:
signature = HMAC-SHA256('ghi', 'abcdef').to_hex
= '52ca0e5beab532532c62155e78d81c7dc8ad6d6f744cf3797668cf52dd2f9a41'
Now we just need to pass the resulting signature back to the server in the following format:
{auth: {access_key: 'your_access_key', answer: 'the_signature'}}
Accept data
After the verification is passed, the real-time data sent by the server can be accepted. Currently, two types of real-time data are provided through the WebSocket API: Trade and Orderbook. The detailed data format is as follows:
Type of data | Data structure / example | Remarks |
---|---|---|
Trade |
| Trade News has a new deal on behalf of your pending order. Price/volume transaction price / number of transactions Funds turnover (in quote currency) Market base currency/quote currency pair Side Your role in the deal (ask: seller, bid: buyer) Ask or bid pending order data after the transaction |
Orderbook |
| The Orderbook message represents a change in the orderbook. The Orderbook add message ("action"=>"add") indicates that a new order has been added to the orderbook. The Orderbook remove message ("action"=>"remove") indicates that the orderbook has removed the order (the reason may be that the user cancels the pending order or the pending order is completely filled). The Orderbook update message ("action"=>"update") indicates that the order in the orderbook has been updated. Action orderbook operation, range of values: add, remove, update Order the order being operated |