You can get Google Docs version here

Along with REST API v3 Stex offers WebSocket API allowing you to subscribe to the public and private channels that provide real time changes of the trading information.

We use Socket.io at the background of the websocket data provider. The examples in this document will be implemented in node.js using the socket.io-client package.

What data are available through the websocket connection

Stex allow to get the following data through the websocket subscription:

Public channels

  • SELL/BUY glasses rows changes
  • SELL/BUY glasses total changes
  • New trades
  • Chart candles updates
  • Ticker updates

Private channels

  • User orders changes
  • User orders deletion (closing/cancelling)
  • New user trades
  • User balance changes

General workflow

General approach of working with Stex websocket is as follows:

  1. Subscribe to named channel
  2. Start listening certain event
  3. Handle the event according to your needs

It is better to look at the simple example:

const

    io = require("socket.io-client"),

    socket = io.connect("https://socket.stex.com");

socket.emit('subscribe', {

    channel: 'rate',

    auth: {}

});

socket.on("App\\Events\\Ticker", function(msg, obj) {

    console.info(obj);

});

This code subscribes to the public channel ‘rate’ and listens to the event ‘App\Events\Ticker’. As soon as event occurs it prints the data to console. 

Events and their corresponding channels you may subscribe to

Even name

App\Events\Ticker

Channel name rules

rate

Description

ticker changes

Channel name example

rate

Authentication

none

Data format

{

  id: currency pair ID,

  lastPrice: price of last trade,

  lastPriceDayAgo: 24 hours opening price,

  maxBuy: maximum trade price for the last 24 hours,

  minSell: minimum trade price for the last 24 hours,

  spread: price change in percents,

  volumeSum: 24 hours volume,

  market_volume: 24 hours volume in market currency,

  closedOrders: number of trades past 24 hours,

  precision: number of valuable digits after comma,

  socket: null

}

Example data

{

  id: 1,

  lastPrice: "0.000137",

  lastPriceDayAgo: "0.00000725",

  maxBuy: "0.000137",

  minSell: "0.00000721",

  spread: "1.6551",

  volumeSum: "3299.86144533",

  market_volume: "0.02458535",

  closedOrders: 33,

  precision: 8,

  socket: null

}

Even name

App\Events\OrderFillCreated

Channel name rules

trade_c{currency_pair_id}

Description

New trades for the given currency pair

Channel name example

trade_c1

Authentication

none

Data format

{

  id: integer trade ID,

  buy_order_id: integer buy order ID,

  sell_order_id: integer sell order ID,

  currency_pair_id: integer currency pair ID,

  price: string trade price,

  amount: string trade amount,

  amount2: trade amount in market currency,

  order_type: string trade type SELL / BUY,

  date: date and time of the trade in UTC,

  timestamp: timestamp of the trade,

  socket: null,

}

Example data

{

  id: 434976,

  buy_order_id: 1015329,

  sell_order_id: 1015330,

  currency_pair_id: 1,

  price: "0.00013700",

  amount: "4.00000000",

  amount2: 0.000548,

  order_type: "SELL",

  date: "17.01.19 10:57:07",

  timestamp: 1547722627,

  socket: null,

}

Even name

App\Events\GlassTotalChanged

Channel name rules

{type}_total_data{currency_pair_id}

Description

Given currency pair sell/buy glass totals changes

Channel name example

sell_total_data1

buy_total_data1

Authentication

none

Data format

{

  currency_pair_id: integer currency pair ID,

  total: string total glass value,

  socket: null

}

Example data

{

  currency_pair_id: 1,

  total: "0.00137",

  socket: null

}

Even name

App\Events\GlassRowChanged

Channel name rules

{type}_data{currency_pair_id}

Description

Given currency pair sell/buy glass row changes.

Returns the added or changed glass row data.

In case the amount is zero this means the row should be removed from the glass

Channel name example

sell_data1

buy_data1

Authentication

none

Data format

{

  currency_pair_id: integer currency pair ID,

  amount: string available amount of the row,

  price: string glass row price,

  amount2: string available amount in market currency,

  count: integer count of the orders with this price,

  socket: null

}

Example data

{

  currency_pair_id: 1,

  amount: "11875.08451705",

  price: "0.00013700",

  amount2: "1.62688658",

  count: 15,

  socket: null

}

Even name

App\Events\BestPriceChanged

Channel name rules

best_{type}_price_{currency_pair_id}

Description

Changes of the best bid/ask price for given currency pair and orders type

Channel name example

best_bid_price_1

best_ask_price_1

Authentication

none

Params description

The “type” stays for bid or ask

Data format

{

  best_price: "0.03138775"

  currency_pair_id: 2

  order_type_id: 2

  timestamp_ms: 1548933805386

  socket: null

}

Example data

{

  best_price: "0.03138775"

  currency_pair_id: 2

  order_type_id: 2

  timestamp_ms: 1548933805386

  socket: null

}

Even name

App\Events\CandleChanged

Channel name rules

stats_data_{chart_type}_{currency_pair_id}

Description

Changes of the chart candles for given currency pair of the given chart type

Channel name example

stats_data_1D_1

Authentication

none

Params description

There is “chart_type” placeholder the channel name. Possible values it can take are:

  • 1 - stays for 1 minute candles
  • 5 - stays for 5 minutes candles
  • 30 - stays for 30 minutes candles
  • 60 - stays for hourly candles
  • 240 - stays for 4 hours candles
  • 720 - stays for 12 hour candles
  • 1D - stays for daily candles

Data format

{

  _id:

  {

    currency_pair_id: currency pair id,

    timestamp: candle timestamp

  },

  open: candle open price,

  close: candle close price,

  low: lowest price for candle period,

  high: highest price for candle period,

  count: trades count for candle period,

  volume: volume of trades for candle period,

  socket: null,

}

Example data

{

  _id:

  {

    currency_pair_id: 1,

    timestamp: 1547683200

  },

  open: 0.000137,

  close: 0.000140,

  low: 0.000136,

  high: 0.000141,

  count: 900,

  volume: 4987.456871,

  socket: null,

}

Even name

App\Events\BalanceChanged

Channel name rules

private-balance_changed_w_{wallet_id}

Description

Balance changes of the given wallet

Channel name example

private-balance_changed_w_1234

Authentication

Access token with push scope

Data format

{

  id: integer wallet id,

  balance: string available balance,

  bonus_balance: string bonus balance,

  currency_code: "BTC",

  frozen_balance: "0.00430520",

  socket: null

}

Example data

{

  id: 27,

  balance: "1.00340283",

  bonus_balance: "0.00000000",

  currency_code: "BTC",

  frozen_balance: "0.00430520",

  socket: null,

}

Even name

App\Events\UserOrder

Channel name rules

private-{type}_user_data_u{user_id}c{currency_pair_id}

Description

User order changes for given currency_pair

Channel name example

private-sell_user_data_u5432c1

private-buy_user_data_u5432c1

Authentication

Access token with push scope

Data format

{

  id: integer order ID,

  user_id: integer user ID,

  currency_pair_id: integer currency pair ID,

  amount: string available order amount,

  price: string order price,

  amount2: string amount in market currency,

  socket: null,

}

Example data

{

  id: 1015329,

  user_id: 5432,

  currency_pair_id: 1,

  amount: "10.00000000",

  price: "0.00013700",

  amount2: "0.00137000",

  socket: null,

}

Even name

App\Events\UserOrderDeleted

Channel name rules

private-del_order_u{user_id}c{currency_pair_id}

Description

Any type of finishing (including the cancelling) of the user orders for given currency pair

Channel name example

private-del_order_u5432c1

Authentication

Access token with push scope

Data format

{

  id: integer order ID,

  user_id: integer user ID,

  currency_pair_id: integer currency pair id

  socket: null,

}

Example data

{

  id: 1015330,

  user_id: 13,

  socket: null,

  currency_pair_id: 1

}

Even name

App\Events\UserOrderFillCreated

Channel name rules

private-trade_u{user_id}c{currency_pair_id}

Description

New trades of the given currency pair for given user

Channel name example

private-trade_u5432c1

Authentication

Access token with push scope

Data format

{

  user_id: integer user ID

  currency_pair_id: integer currency pair ID

  price: string trade price

  amount: string trade amount

  amount2: string trade amount in market currency

  date: date and time of the trade in UTC

  order_type: string trade type (Sell / Buy)

  socket: null

}

Example data

{

  user_id: 5432

  currency_pair_id: 1

  price: "0.00013700"

  amount: "4.00000000"

  amount2: 0.000548

  date: "17.01.19 10:57:07"

  order_type: "Sell"

  socket: null

}

Subscribing to private channels

You have to send access token in subscription request headers in order to subscribe to private channels.

This example shows how to do this

const

    io = require("socket.io-client"),

    socket = io.connect("https://socket.stex.com"),

    token = "eyG0eSAiOiQKV1QtLCJhbGciOiJSUzI1NiIsImp0aSI6ImY1ODVjODNmYzczNmM4ODk4ZjY5MDM1Y2E1M2U1YTkwOGEwNGM1YTNhMGU2MTUxMDc2MzkwMGYwYTAwN2U0NzU3NzMyZDQ4ZDJkMmZhYzg3In0.eyJhdWQiOiI5IiwianRpIjoiZjU4NWM4M2ZjNzM2Yzg4OThmNjkwMzVjYTUzZTVhOTA4YTA0YzVhM2EwZTYxNTEwNzYzOTAwZjBhMDA3ZTQ3NTc3MzJkNDhkMmQyZmFjODciLCJpYXQiQjZ1NDc2NDY2NzMsIm5iZiI6MTU0NzY0NjY3MywiZXhwIjoxNTQ4MDc4NjczLCJzdWIiOiIxMyIsInNjb3BlcyI6WyJwdXNoIl19.WG-9g4fGsR1iULZkQZ6E5wQPZk5Q94X6d--99TXFmjPs7iAnvikSw8Q0VoADyOMnsJfnflqPn_uKOozpRTvcJ6QyLxa8KeGAd_NKM5I4fZKom8tZXtJQh6aWE8ClJJM01hjKu8DIqh6eQJEm2SQlwIUU76-NsRK-NNYyKqPpUVhH6tmiUOvuVDHNeaaF7dMV1yh6sC9fiTqC6-Nc3SwyNxeaSOXXo3NJOPLDIWJp-sq69aS1qUCl6euIJt-TIhoOfgCWJJ-Bu156BttIV5fSfMKn3Wl-XoHFAxmtGzCPzvYSeuIYxdguYQLhiSb_AbR10bfgt6d3VGcEU06qXtREuCrMerCoP17UQBLoFr7EL6N2w-rSWkx4jSIe5hyOcpCVW288CWw0a6-5lRXkADL6X7O6qpTVSghxMBiT5zwSBqOxVmfVe_uzeJovRewiAqJBwWQAioqZqJtBQFvB09yRLW22l_z2jbVicohlXMp_uiyp6hjLkwGps2v66dnQVhP95o0S1nc91f2XTuW0n1RAvxASRJzzwkx0Jo9_m0DvSmBnf6hu-N8TSX7pgfRrc9tfPRN66eCoCkxY2g64MbMqw1wtrUY9dxzLy5bGa7NVMppfrJqwUbc6ohvFAtZvrdk4OsgqFbZVEKOKI73atZcraiaFjUOrWM3DBfuFuRDQv50";

socket.emit('subscribe', {

    channel: 'private-buy_user_data_u5432c1',

    auth: {headers: {Authorization: 'Bearer ' + token}}

});

socket.on("App\\Events\\UserOrder", function(msg, obj) {

    console.info(obj);

});

See the token constant defined in the top of the script. This is access token you obtained from the REST API v3 (see the examples in the “How to connect to the stex api v3 using Postman” document).

This token then should be passed with bearer auth:

auth: {headers: {Authorization: 'Bearer ' + token}}

In real application this token should be properly handled and renewed when needed with refresh token.

You can download and try examples in this document from this link

To run the code:

  1. unzip the contents to any folder
  2. run npm install within this folder
  3. Fill in the user_id, currency_pair_id and token values in the test.js file

node test.js will start the application

Did this answer your question?