logo

Peatio WebSocket API

Peatio WebSocket API connections are handled by Ranger service provided by peatio gem.

API

There are two types of channels:

  • Public: accessible by anyone
  • Private: accessible only by given member

GET request parameters:

FieldDescriptionMultiple allowed
streamList of streams to be subscribed onYes

List of supported public streams:

List of supported private streams (requires authentication):

You can find a format of these events below in the doc.

#Public channels architecture

scheme

#Private channels architecture

scheme

Authentication

Authentication happens on websocket message with following JSON structure.

{
  "jwt": "Bearer <Token>"
}

If authenticaton was done, server will respond successfully

{
  "success": {
    "message": "Authenticated."
  }
}

Otherwise server will return an error

{
  "error": {
    "message": "Authentication failed."
  }
}

If authentication JWT token has invalid type, server return an error

{
  "error": {
    "message": "Token type is not provided or invalid."
  }
}

If other error occured during the message handling server throws an error

{
  "error": {
    "message": "Error while handling message."
  }
}

Note: Peatio websocket API supports authentication only Bearer type of JWT token.

Example of authentication message:

{
  "jwt": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
}

When user successfully authenticated server subsribes client to given streams passed as GET request parameter ?stream= (can be specified multiple times)

When order or trade done, websocket server send message to client with object details.

Depending on what trade happend server will send the ask and bid details.

Public streams

Update

Here is structure of <market.update> event:

FieldDescription
asksAdded asks with price and total volume expose as array.
bidsAdded bids with price and total volume expose as array.

Example:

{
  asks: [[0.4e1, 0.1e-1], [0.3e1, 0.401e1]], # first is price & second is total volume
  bids: [[0.5e1, 0.4e1]]
}

Trades

Here is structure of <market.trades> event expose as array with trades:

FieldDescription
tidUnique trade tid.
taker_typeTaker type of trade, either buy or sell.
pricePrice for the trade.
amountThe amount of trade.
created_atTrade create time.

Kline point

Kline point as array of numbers:

  1. Timestamp.
  2. Open price.
  3. Max price.
  4. Min price.
  5. Last price.
  6. Period volume

Example:

[1537370580, 0.0839, 0.0921, 0.0781, 0.0845, 0.5895]

Tickers

Here is structure of global.tickers event expose as array with all markets pairs:

FieldDescription
atDate of current ticker.
nameMarket pair name.
base_unitBase currency.
quote_unitQuote currency.
lowLowest price in 24 hours.
highHighest price in 24 hours.
lastLast trade price.
openLast trade from last timestamp.
closeLast trade price.
volumeVolume in 24 hours.
sellBest price per unit.
buyBest price per unit.
avg_priceAverage price for last 24 hours.
price_change_percentAverage price change in percent.

Private streams

Order

Here is structure of Order event:

FieldDescription
idUnique order id.
marketThe market in which the order is placed. (In peatio market_id)
sideType of order, either but or sell.
ord_typeOrder tyep, either limit or market.
priceOrder price.
avg_priceOrder average price.
stateOne of wait, done, or cancel.
origin_volumeThe amount user want to sell/buy.
remaining_volumeRemaining amount user want to sell/buy.
executed_volumeExecuted amount for current order.
created_atOrder create time.
updated_atOrder create time.
trades_countTrades wiht this order.
kindType of order, either bid or ask. (Deprecated)
atOrder create time. (Deprecated) (In peatio created_at)

Trade

Here is structure of Trade event:

FieldDescription
idUniq trade id.
pricePrice for each unit.
amountThe amount of trade.
totalThe total of trade (volume * price).
marketThe market in which the trade is placed. (In peatio market_id)
sideType of order in trade that related to current user sell or buy.
taker_typeTaker type of trade, either buy or sell.
created_atTrade create time.
order_idUser order identifier in trade.

Development

Start ranger websocket server using following command in peatio-core gem:

$ ./bin/peatio service start ranger

Now we can test authentication with wscat:

Connect to public channel:

$ wscat -n -c 'ws://ws.app.local:8080/api/ranger/v2?stream=usdeth'

Connect to private channel:

Authorization header will be injected automatically by ambassador so we could subscribe to private channels.

$ wscat -n -c 'ws://ws.app.local:8080/api/ranger/v2?stream=trade'

Examples

There is also example of working with Ranger service using NodeJS.