STEX websocket subscription

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 to 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 the event occurs it prints the data to the console.

Events and their corresponding channels you may subscribe to

Event name

App\Events\Ticker

Channel name rules

rate_{currency_pair_id}

Description

ticker changes for a single given market

Channel name example

rate_2

Authentication

none

Data format

{
id: currency pair ID,
lastPrice: price of the 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 percent,
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
}

Event name

App\Events\Ticker

Channel name rules

rate

Description

ticker changes for all markets

Channel name example

rate

Authentication

none

Data format

{
id: currency pair ID,
lastPrice: price of the 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 percent,
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
}

Event 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,
}

Event 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
}

Event name

App\Events\GlassRowChanged

Channel name rules

{type}_data{currency_pair_id}

Description

Given currency pair sell or buy glass row changes (separately sell and buy).

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,
type: string “SELL” or “BUY”
socket: null
}

Example data

{
currency_pair_id: 1,
amount: "11875.08451705",
price: "0.00013700",
amount2: "1.62688658",
count: 15,
type: "SELL",
socket: null
}

Event name

App\Events\GlassRowChanged

Channel name rules

orderbook_data{currency_pair_id}

Description

Given currency pair sell and buy glass row changes (sell and buy in one subscription).

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

orderbook_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,
type: string “SELL” or “BUY”
socket: null
}

Example data

{
currency_pair_id: 1,
amount: "11875.08451705",
price: "0.00013700",
amount2: "1.62688658",
count: 15,
type: "SELL",
socket: null
}

Event 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
}

Event 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,
}

Event 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",
hold_balance: "0.00000000",
socket: null
}

Example data

{
id: 27,
balance: "1.00340283",
bonus_balance: "0.00000000",
currency_code: "BTC",
frozen_balance: "0.00430520",
hold_balance: "0.00000000",
socket: null,
}

Event name

App\Events\BalanceChanged

Channel name rules

private-balance_changed_u_{user_id}

Description

Balance changes of any wallet of the given user

Channel name example

private-balance_changed_u_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",
hold_balance: "0.00000000",
socket: null
}

Example data

{
id: 27,
balance: "1.00340283",
bonus_balance: "0.00000000",
currency_code: "BTC",
frozen_balance: "0.00430520",
hold_balance: "0.00000000",
socket: null,
}

Event 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,
currency_pair_name: string pair name,
status: string current status name,
amount: string available order amount,
initial_amount: string initial order amount,
processed_amount: string executed order amount,
price: string order price,
amount2: string amount in market currency,
type: string,
created: date time string of order created date,
timestamp: order creation timestamp,
socket: null,
}

Example data

{
id: 1015329,
user_id: 5432,
currency_pair_id: 1,
currency_pair_name: "ETH/BTC",
status: "PARTIAL",
amount: "10.00000000",
initial_amount: "15.00000000",
processed_amount: "5.00000000",
price: "0.00013700",
amount2: "0.00137000",
type: "SELL",
created: "2020-07-08 13:56:08",
timestamp: 1594205768,
socket: null,
}

Event name

App\Events\UserOrder

Channel name rules

private-order_user_data_u{user_id}c{currency_pair_id}

Description

User order changes for given currency_pair

Channel name example

private-order_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,
currency_pair_name: string pair name,
status: string current status name,
amount: string available order amount,
initial_amount: string initial order amount,
processed_amount: string executed order amount,
price: string order price,
amount2: string amount in market currency,
type: string,
created: date time string of order created date,
timestamp: order creation timestamp,
socket: null,
}

Example data

{
id: 1015329,
user_id: 5432,
currency_pair_id: 1,
currency_pair_name: "ETH/BTC",
status: "PARTIAL",
amount: "10.00000000",
initial_amount: "15.00000000",
processed_amount: "5.00000000",
price: "0.00013700",
amount2: "0.00137000",
type: "SELL",
created: "2020-07-08 13:56:08",
timestamp: 1594205768,
socket: null,
}

Event name

App\Events\UserOrder

Channel name rules

private-user_orders_u_{user_id}

Description

User orders changes for any currency_pair

Channel name example

private-user_orders_u_5432c1

Authentication

Access token with push scope

Data format

{
id: integer order ID,
user_id: integer user ID,
currency_pair_id: integer currency pair ID,
currency_pair_name: string pair name,
status: string current status name,
amount: string available order amount,
initial_amount: string initial order amount,
processed_amount: string executed order amount,
price: string order price,
amount2: string amount in market currency,
type: string,
created: date time string of order created date,
timestamp: order creation timestamp,
socket: null,
}

Example data

{
id: 1015329,
user_id: 5432,
currency_pair_id: 1,
currency_pair_name: "ETH/BTC",
status: "PARTIAL",
amount: "10.00000000",
initial_amount: "15.00000000",
processed_amount: "5.00000000",
price: "0.00013700",
amount2: "0.00137000",
type: "SELL",
created: "2020-07-08 13:56:08",
timestamp: 1594205768,
socket: null,
}

Event 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,
currency_pair_name: string pair name,
status: string current status name,
amount: string available order amount,
initial_amount: string initial order amount,
processed_amount: string executed order amount,
price: string order price,
amount2: string amount in market currency,
type: string,
created: date time string of order created date,
timestamp: order creation timestamp,
socket: null,
}

Example data

{
id: 1015329,
user_id: 5432,
currency_pair_id: 1,
currency_pair_name: "ETH/BTC",
status: "PARTIAL",
amount: "10.00000000",
initial_amount: "15.00000000",
processed_amount: "5.00000000",
price: "0.00013700",
amount2: "0.00137000",
type: "SELL",
created: "2020-07-08 13:56:08",
timestamp: 1594205768,
socket: null,
}

Event name

App\Events\UserOrderDeleted

Channel name rules

private-del_order_u_{user_id}

Description

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

Channel name example

private-del_order_u_5432

Authentication

Access token with push scope

Data format

{
id: integer order ID,
user_id: integer user ID,
currency_pair_id: integer currency pair ID,
currency_pair_name: string pair name,
status: string current status name,
amount: string available order amount,
initial_amount: string initial order amount,
processed_amount: string executed order amount,
price: string order price,
amount2: string amount in market currency,
type: string,
created: date time string of order created date,
timestamp: order creation timestamp,
socket: null,
}

Example data

{
id: 1015329,
user_id: 5432,
currency_pair_id: 1,
currency_pair_name: "ETH/BTC",
status: "PARTIAL",
amount: "10.00000000",
initial_amount: "15.00000000",
processed_amount: "5.00000000",
price: "0.00013700",
amount2: "0.00137000",
type: "SELL",
created: "2020-07-08 13:56:08",
timestamp: 1594205768,
socket: null,
}

Event 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
}

Event name

App\Events\ReportSuccess

Channel name rules

private-report_success_{user_id}

Description

Fires on successful report finishing (see reports)

Channel name example

private-report_success_5432

Authentication

Access token with push scope

Data format

{
id: integer report ID
socket: null
}

Example data

{
id: 5487
socket: null
}

Event name

App\Events\NotifyCount

Channel name rules

private-notify_user_{user_id}_about_count

Description

Notifications count change for the user

Channel name example

private-notify_user_5432_about_count

Authentication

Access token with push scope

Data format

{
count: integer count unread messages
id: uuid message
title: string message
socket: null
}

Example data

{
count: 5
id: "63f507dc-58a1-4552-9acc-a020a1b457ce"
title: "Finished order"
socket: null
}

Event name

App\Events\DepositUpdated

Channel name rules

private-deposits_u_{user_id}

Description

Changes of the deposits of the user (mainly for status or confirmation updates)

Channel name example

private-deposits_u_5432

Authentication

Access token with push scope

Data format

{
id: integer deposit ID
currency_id: integer currency ID
currency_code: string currency code
deposit_fee_currency_id: integer currency ID
deposit_fee_currency_code: string currency code
amount: string amount
fee: string fee amount
txid: string network transaction ID
protocol_id: integer used protocol ID (for USDT)
deposit_status_id: integer deposit status ID
status: string deposit status name
status_color: string color code for the status
created_at: date and time of the deposit
timestamp: integer timestamp of the deposit
confirmations: string confirmations received
protocol_specific_settings:
{
protocol_name: string protocol name,
protocol_id: integer protocol id,
block_explorer_url: string
}
socket: null
}

Example data

{
id: 123
currency_id: 1
currency_code: BTC
deposit_fee_currency_id: 1
deposit_fee_currency_code: BTC
amount: "0.5"
fee: "0.0"
txid: "asasf423dsd231fsasas121212"
protocol_id: 0
deposit_status_id: 1
status: "PROCESSING"
status_color: "#BC3D51"
created_at: "2020-07-08 13:56:08",
timestamp: 1594205768,
confirmations: "1 of 2"
protocol_specific_settings:
{
protocol_name: "",
protocol_id: 0,
block_explorer_url: ""
}
socket: null
}

Event name

App\Events\WithdrawalUpdated

Channel name rules

private-withdrawals_u_{user_id}

Description

Changes of the withdrawals of the user (mainly for status or confirmation updates)

Channel name example

private-withdrawals_u_5432

Authentication

Access token with push scope

Data format

{
id: integer withdrawal ID
currency_id: integer currency ID
currency_code: string currency code
fee_currency_id: integer currency ID
fee_currency_code: string currency code
amount: string amount
fee: string fee amount
txid: string network transaction ID
protocol_id: integer used protocol ID (for USDT)
withdrawal_status_id: integer withdrawal status ID
status: string withdrawal status name
status_color: string color code for the status
created_at: date and time of the withdrawal created
created_ts: integer timestamp of the withdrawal created
updated_at: date and time of the withdrawal last changed (e.g. status)
updated_ts: integer timestamp of the withdrawal last changed (e.g. status)
protocol_specific_settings:
{
protocol_name: string protocol name,
protocol_id: integer protocol id,
block_explorer_url: string
}
withdrawal_address:
{
address: string,
address_name: string,
additional_address_parameter: string
additional_address_parameter_name: string
protocol_name: string protocol name,
protocol_id: integer protocol id,
supports_new_address_creation: boolean
}
socket: null
}

Example data

{
id: 123
currency_id: 1
currency_code: BTC
fee_currency_id: 1
fee_currency_code: BTC
amount: "0.5"
fee: "0.0"
txid: "asasf423dsd231fsasas121212"
protocol_id: 0
withdrawal_status_id: 3
status: "PROCESSING"
status_color: "#BC3D51"
created_at: "2020-07-08 13:56:08",
created_ts: 1594205768,
updated_at: "2020-07-08 13:56:08",
updated_ts: 1594205768,
confirmations: "1 of 2"
protocol_specific_settings:
{
protocol_name: "",
protocol_id: 0,
block_explorer_url: ""
}
withdrawal_address:
{
address: "abcdea2121sdasad112121212asasas",
address_name: "Address",
additional_address_parameter: ""
additional_address_parameter_name: ""
protocol_name: "",
protocol_id: 0,
supports_new_address_creation: true
}
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.

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

  4. node test.js will start the application

Did this answer your question?