WebSocket Data Interface (WebSocket API)

Interface URI: wss://www.nlexch.com:8080
Return the result format: JSON
WebSocket API

The WebSocket API provides a more real-time data access. Your server can change the orderbook turnover and market data connections in real-time via websocket (push) to the user WebSocket API's.

The WebSocket API can be divided into three simple steps:

  1. 1. Establishing of websocket connection
  2. 2. Use api token to verify identity
  3. 3. Receiving and processing data
Establishing of websocket connection

WebSocket is a widely used as standard and supports a variety of languages. In Javascript for example:

Javascript:

var host = 'wss://www.nlexch.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");
}

You can also refer to the implementation of the official Ruby client:https://github.com/amarian12/peatio-client-ruby/blob/master/lib/peatio_api/streaming_client.rb

Use api token to verify identity

Prior to access, you must prepare your access/secret key. After registration by simply accessing the API Tokens page you can get your key.

Once you establish a WebSocket connection with the server, the server returns a message in the following format:

{"challenge":"d45sSFIZZdYzRgwi-zDqA8HFP2MfVoWqXlHX-2LbB_37q9_3pkZ8og"}

The challenge contains a randomly generated string which the client needs to use access/secret key signature of this string, and then signing back to the server, which verifies the signature, then continue to the next step. Otherwise it returns an error message.

Signature generation is very simple, first put your access key and challenge string concatenation:

payload = access_key + challenge

Assuming access key for the "abc", challenge for the "def", the payload is "abcdef" followed by HMAC-SHA256 and secret key computing payload signature:

signature = HMAC-SHA256(secret_key, payload).to_hex

Assuming secret_key for 'ghi', using HMAC-SHA256 for the signature on the case of the payload results:

signature = HMAC-SHA256('ghi', 'abcdef').to_hex
          = '52ca0e5beab532532c62155e78d81c7dc8ad6d6f744cf3797668cf52dd2f9a41'

Now we just need to get the signature back to the server in the following format:

{auth: {access_key: 'your_access_key', answer: 'the_signature'}}

For more information you can refer to the brave official Ruby client implementation:https://github.com/amarian12/peatio-client-ruby/blob/master/lib/peatio_api/streaming_client.rb

Receiving and processing data

After validation by real-time data can be sent by the server to accept the current offer two types of real-time data via brave WebSocket API:. Trade and Orderbook detailed data format is as follows:

TypeData structure / exampleRemarks
Trade
{ "trade"=>
    { "id"=>2435,
      "price"=>"3500.0",
      "volume"=>"0.0331",
      "funds"=>"115.85",
      "market"=>"ltcbtc",
      "created_at"=>"2016-08-16T05:02:37Z",
      "side"=>"bid",
      "bid"=>{
        "id"=>3248, "side"=>"buy", "price"=>nil,
        "avg_price"=>"3500.0", "state"=>"done", "market"=>"ltcbtc",
        "created_at"=>"2016-08-16T05:02:37Z", "volume"=>"0.0331",
        "remaining_volume"=>"0.0", "executed_volume"=>"0.0331"
      }
    }
}

Trade messages on behalf of your pending order a new deal.

price / volume: price / number of transactions

funds: Turnover (to quote currency)

market: market: the base currency / Quote currency

side: your role in this transaction in the (ask: seller, bid: buyer)

ask or bid: ask or bid: Transaction data after lodging

Orderbook
{ "orderbook"=>
  { "action"=>"add",
    "order"=>{
      "id"=>3252,
      "timestamp"=>1402898864,
      "type"=>"ask",
      "volume"=>"1.0",
      "price"=>"3500.0",
      "market"=>"ltcbtc",
      "ord_type"=>"limit"}
  }
}

Orderbook message representative orderbook has changed.

Orderbook add a message ("action" => "add") to indicate a new order to join orderbook.

Orderbook remove the message ("action" => "remove") indicates orderbook removed this order (may be due to the user cancels the pending order or a pending order completely turnover).

Orderbook update messages ("action" => "update") indicates orderbook in order to be updated.

action: orderbook operating ranges: add, remove, update

order: the order being operated

Related library / tool list