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:
Subscribe to named channel
Start listening to certain event
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 |
|
Description | ticker changes for a single given market |
Channel name example | rate_ |
Authentication | none |
Data format |
|
Example data |
|
Event name | App\Events\Ticker |
Channel name rules | rate |
Description | ticker changes for all markets |
Channel name example | rate |
Authentication | none |
Data format |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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:
|
Data format |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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 |
|
Example data |
|
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:
unzip the contents to any folder
run
npm install
within this folderFill in the
user_id
,currency_pair_id
and token values in thetest.js
filenode test.js
will start the application