-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
ws-public.ts
178 lines (150 loc) · 3.69 KB
/
ws-public.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { DefaultLogger, WebsocketClient } from '../src';
// or use the module installed via `npm install okx-api`:
// import { WebsocketClient, DefaultLogger } from 'okx-api';
// or if you're not using typescript (e.g. pure nodejs), change the "import" to "require":
// const { WebsocketClient, DefaultLogger } = require('okx-api');
// Optional: Inject a custom logger.
// This example overrides the default logger to also log "silly" (super verbose) messages, which are disabled by default
const logger = {
...DefaultLogger,
// silly: (...params) => console.log('silly', ...params),
};
const wsClient = new WebsocketClient(
{
// prod is used by default, but you can choose other markets through this parameter:
// market: 'prod',
// market: 'aws',
// market: 'demo',
},
logger // Optional: inject the custom logger here
);
// Raw data will arrive on the 'update' event
wsClient.on('update', (data) => {
console.log(
new Date(),
'ws update (raw data received)',
JSON.stringify(data, null, 2)
);
// console.log('ws update (raw data received)', JSON.stringify(data, null, 2));
});
wsClient.on('open', (data) => {
console.log('ws connection opened open:', data.wsKey);
});
// Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' event
wsClient.on('response', (data) => {
console.log('ws response received: ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnect', ({ wsKey }) => {
console.log('ws automatically reconnecting.... ', wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('ws has reconnected ', data?.wsKey);
});
wsClient.on('error', (data) => {
console.error('ws exception: ', data);
});
// Send one topic at a time
wsClient.subscribe({
channel: 'instruments',
instType: 'FUTURES',
});
// Or an array of requests
wsClient.subscribe([
{
channel: 'instruments',
instType: 'SPOT',
},
{
channel: 'tickers',
instId: 'LTC-BTC',
},
]);
/**
*
* Examples for each channel: https://www.okx.com/docs-v5/en/#websocket-api-public-channel
*
*/
// Instruments channel
wsClient.subscribe({
channel: 'instruments',
instType: 'SPOT',
});
// Tickers channel
wsClient.subscribe({
channel: 'tickers',
instId: 'BTC-USDT',
});
// Open interest channel
wsClient.subscribe({
channel: 'open-interest',
instId: 'BTC-USD-SWAP',
});
// Candlesticks channel
wsClient.subscribe({
channel: 'candle1m',
instId: 'BTC-USDT',
});
// Trades channel
wsClient.subscribe({
channel: 'trades',
instId: 'BTC-USDT',
});
// Estimated delivery/exercise price channel
wsClient.subscribe({
channel: 'estimated-price',
instType: 'FUTURES',
instFamily: 'BTC-USD',
});
// Mark price channel
wsClient.subscribe({
channel: 'mark-price',
instId: 'BTC-USDT',
});
// Mark price candlesticks channel
wsClient.subscribe({
channel: 'mark-price-candle1m',
instId: 'BTC-USDT',
});
// Price limit channel
wsClient.subscribe({
channel: 'price-limit',
instId: 'LTC-USD-190628',
});
// Order book channel
wsClient.subscribe({
channel: 'books',
instId: 'BTC-USDT',
});
// OPTION summary channel
wsClient.subscribe({
channel: 'opt-summary',
instFamily: 'BTC-USD',
});
// Funding rate channel
wsClient.subscribe({
channel: 'funding-rate',
instId: 'BTC-USD-SWAP',
});
// Index candlesticks channel
wsClient.subscribe({
channel: 'index-candle1m',
instId: 'BTC-USD',
});
// Index tickers channel
wsClient.subscribe({
channel: 'index-tickers',
instId: 'BTC-USDT',
});
// Status channel
wsClient.subscribe({
channel: 'status',
});
// Liquidation orders channel
wsClient.subscribe({
channel: 'liquidation-orders',
instType: 'FUTURES',
});
wsClient.subscribe({
channel: 'liquidation-orders',
instType: 'SWAP',
});