forked from tiagosiebler/ftx-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websockets.ts
68 lines (58 loc) · 1.48 KB
/
websockets.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
import { isWsTradesEvent } from "../src/util/typeGuards";
import { WebsocketClient } from "../src/websocket-client";
/*
Demonstrations on connecting to private & public websocket channels
*/
try {
const params = {
key: 'apikeyhere',
secret: 'apisecrethere',
// subAccountName: 'sub1',
}
// Prepare a ws connection (connection init is automatic once ws client is instanced)
const ws = new WebsocketClient(params);
// append event listeners
ws.on('response', msg => console.log('response: ', msg));
ws.on('update', msg => {
// use a type guard to narrow down types
if (isWsTradesEvent(msg)) {
// msg now is WsEventTrades
console.log('trades event: ', msg);
} else {
console.log('update: ', msg);
}
});
ws.on('error', msg => console.log('err: ', msg));
// Subscribe to public & private topics. Any of the following are valid:
/*
Option 1: specify channel using name (no params)
*/
ws.subscribe('fills');
/*
Option 2: specify list of channel names (no params)
*/
ws.subscribe(['fills', 'orders']);
/*
Option 3: specify channel with extra parameters
*/
ws.subscribe({
channel: 'trades',
market: 'BTC-PERP'
});
/*
Option 4: specify channel with extra parameters
*/
ws.subscribe([
{
channel: 'trades',
market: 'BTC-PERP'
},
{
channel: 'orderbookGrouped',
market: 'BTC-PERP',
grouping: 500
}
]);
} catch (e) {
console.error('err: ', e.body);
}