-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathws-private.ts
54 lines (46 loc) · 1.36 KB
/
ws-private.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
import { WebsocketClient, WS_KEY_MAP, DefaultLogger } from '../src';
// or
// import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api';
(async () => {
const logger = {
...DefaultLogger,
silly: () => {},
};
const key = process.env.API_KEY;
const secret = process.env.API_SECRET;
// USDT Perps:
const market = 'linear';
// Inverse Perp
// const market = 'inverse';
// const market = 'spot';
// Note: the WebsocketClient defaults to testnet. Set `livenet: true` to use live markets.
const wsClient = new WebsocketClient(
{
key: key,
secret: secret,
market: market,
// testnet: true,
restOptions: {
// enable_time_sync: true,
},
},
logger
);
wsClient.on('update', (data) => {
console.log('raw message received ', JSON.stringify(data, null, 2));
});
wsClient.on('open', (data) => {
console.log('connection opened open:', data.wsKey);
});
wsClient.on('response', (data) => {
console.log('ws response: ', 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);
});
// subscribe to private endpoints
wsClient.subscribe(['position', 'execution', 'order', 'wallet']);
})();