-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
ws-public-spot-trades.ts
55 lines (47 loc) · 1.27 KB
/
ws-public-spot-trades.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
import { WebsocketClient, DefaultLogger, isWsFormattedTrade } from '../src';
// or, with the npm package
/*
import {
WebsocketClient,
DefaultLogger,
isWsFormattedTrade,
} from 'binance';
*/
(async () => {
const logger = {
...DefaultLogger,
// silly: () => {},
};
const wsClient = new WebsocketClient(
{
beautify: true,
},
logger
);
wsClient.on('formattedMessage', (data) => {
if (isWsFormattedTrade(data)) {
console.log('trade event ', data);
return;
}
console.log('log formattedMessage: ', data);
});
wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey, data.ws.target.url);
});
wsClient.on('reply', (data) => {
console.log('log reply: ', JSON.stringify(data, null, 2));
});
wsClient.on('reconnecting', (data) => {
console.log('ws automatically reconnecting.... ', data?.wsKey);
});
wsClient.on('reconnected', (data) => {
console.log('ws has reconnected ', data?.wsKey);
});
// Request subscription to the following symbol trade events:
const symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT'];
// Loop through symbols
for (const symbol of symbols) {
console.log('subscribing to trades for: ', symbol);
wsClient.subscribeSpotTrades(symbol);
}
})();