forked from tiagosiebler/kucoin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-spot-public.ts
149 lines (132 loc) · 4.12 KB
/
ws-spot-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
/* eslint-disable @typescript-eslint/no-unused-vars */
import {
DefaultLogger,
WebsocketClient,
WsTopicRequest,
} from '../src/index.js';
// import { DefaultLogger, WebsocketClient, WsTopicRequest } from 'kucoin-api';
// normally you should install this module via npm: `npm install kucoin-api`
async function start() {
// Optional: fully customise the logging experience by injecting a custom logger
// const logger: typeof DefaultLogger = {
// ...DefaultLogger,
// trace: (...params) => {
// if (
// [
// 'Sending ping',
// 'Sending upstream ws message: ',
// 'Received pong',
// ].includes(params[0])
// ) {
// return;
// }
// console.log('trace', params);
// },
// };
// const client = new WebsocketClient({}, logger);
const client = new WebsocketClient();
client.on('open', (data) => {
console.log('open: ', data?.wsKey);
});
// Data received
client.on('update', (data) => {
console.info('data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconenct
client.on('reconnect', (data) => {
console.log('reconnect: ', data);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log('reconnected: ', data);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error('close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info('response: ', data);
// throw new Error('res?');
});
client.on('exception', (data) => {
console.error('exception: ', {
msg: data.msg,
errno: data.errno,
code: data.code,
syscall: data.syscall,
hostname: data.hostname,
});
});
try {
// Optional: await a connection to be ready before subscribing (this is not necessary)
// await client.connect('spotPublicV1');
/**
* Use the client subscribe(topic, market) pattern to subscribe to any websocket topic.
*
* You can subscribe to topics one at a time or many one one request. Topics can be sent as simple strings:
*
*/
client.subscribe('/market/ticker:BTC-USDT,ETH-USDT', 'spotPublicV1');
client.subscribe('/market/snapshot:KCS-BTC', 'spotPublicV1');
/**
* Or, as an array of simple strings
*
*/
client.subscribe(
['/market/ticker:BTC-USDT,ETH-USDT', '/market/snapshot:KCS-BTC'],
'spotPublicV1',
);
/**
* Or send a more structured object with parameters
*
*/
const subRequest: WsTopicRequest = {
topic: '/market/ticker:BTC-USDT',
/** Anything in the payload will be merged into the subscribe "request", allowing you to send misc parameters supported by the exchange.
* For more info on parameters, see: https://www.kucoin.com/docs/websocket/basic-info/subscribe/introduction
*/
payload: {
id: 123456,
response: false,
},
};
client.subscribe(subRequest, 'spotPublicV1');
/**
* Or, send an array of structured objects with parameters, if you wanted to send multiple in one request
*
*/
// client.subscribe([subRequest1, subRequest2, etc], 'spotPublicV1');
/**
* Other spot websocket topics:
*/
client.subscribe(
[
'/market/ticker:BTC-USDT,ETH-USDT',
'/market/ticker:all',
'/market/snapshot:KCS-BTC',
'/market/snapshot:BTC',
'/spotMarket/level1:BTC-USDT,ETH-USDT',
'/market/level2:BTC-USDT,ETH-USDT',
'/spotMarket/level2Depth5:BTC-USDT,ETH-USDT',
'/spotMarket/level2Depth50:BTC-USDT,ETH-USDT',
'/market/candles:BTC-USDT_1hour',
'/market/match:BTC-USDT,ETH-USDT',
],
'spotPublicV1',
);
/**
* Other margin websocket topics, which also use the "spotPublicV1" WsKey:
*/
client.subscribe(
[
'/indicator/index:USDT-BTC,ETH-USDT',
'/indicator/markPrice:USDT-BTC,ETH-USDT',
],
'spotPublicV1',
);
} catch (e) {
console.error(`Subscribe exception: `, e);
}
}
start();