Node.js websocket client for FTX. Websocket API documentation: https://docs.ftx.com/#websocket-api
This package is made by the IOfate company and is open source, feel free to use it, share it and contribute!
- Price ticks
- Subscription
- Unsubscribe
- Trades
- Subscription
- Unsubscribe
- Candlesticks (emulated)
- Subscription
- Unsubscribe
- Send ping
- Emit errors by sockets
- Auto-reconnect
$ npm install @iofate/ftx-ws
import { FtxWS } from '@iofate/ftx-ws';
const main = async () => {
const client = new FtxWS();
const symbol = 'BTC/USDT';
await client.connect();
client.on(`ticker-${symbol}`, ticker => console.log(ticker));
client.on('error', error => console.error(error));
client.subscribeTicker(symbol);
};
main();
This package export one class FtxWS
and extend from Emittery, which allow us to dispatch and listen events.
More information about Emittery API here: https://github.com/sindresorhus/emittery#api
There are several events which are emitted in different contexts.
subscriptions
: emitted when a new subscription is made or closedsocket-not-ready
: emitted when the socket is not ready to subscribe (will try to subscribe later)reconnect
: emitted when a reconnection occurredretry-subscription
: emitted event when we retry to subscribeerror
: emitted when an error occurredreconnect-candle
: emitted when a candle socket reconnect. Value emitted:{ symbol: string; interval: string; }
import { FtxWS } from '@iofate/ftx-ws';
const client = new FtxWS();
client.on('subscriptions', subscriptions => console.log('update on subscriptions', subscriptions));
client.on('reconnect', () => console.log('a reconnection occurred'));
client.on('socket-not-ready', msg => console.warn('socket not ready', msg));
client.on('error', error => console.error(error));
Create a new instance of FtxWS.
Subscribe to the websocket ticker of the chosen symbol. Once called you'll be able to listen to ticker events for this symbol.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTicker('BTC/USDT');
ftxWS.on('ticker-BTC/USDT', ticker => console.log(ticker));
Subscribe to the websocket ticker of a symbol list. Once called you'll be able to listen to ticker events for all of those symbols.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTickers(['BTC/USDT', 'ETH/USDT']);
ftxWS.on('ticker-BTC/USDT', ticker => console.log(ticker));
ftxWS.on('ticker-ETH/USDT', ticker => console.log(ticker));
Unsubscribe from the ticker websocket of the associated symbol. Once called no more events will be dispatched.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTicker('BTC/USDT');
const stopListenFn = ftxWS.on('ticker-BTC/USDT', ticker => console.log(ticker));
ftxWS.unsubscribeTicker('BTC/USDT');
stopListenFn();
Unsubscribe from the ticker websocket for the list of symbols. Once called no more events will be dispatched.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTickers(['BTC/USDT', 'ETH/USDT']);
ftxWS.unsubscribeTickers(['BTC/USDT', 'ETH/USDT']);
Subscribe to the trades websocket of the chosen symbol. Once called you'll be able to listen to trades events for this symbol.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTrades('BTC/USDT');
ftxWS.on('trades-BTC/USDT', trades => console.log(trades));
Unsubscribe from the trades websocket of the associated symbol. Once called no more events will be dispatched.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeTrades('BTC/USDT');
const stopListenFn = ftxWS.on('trades-BTC/USDT', trades => console.log(trades));
ftxWS.unsubscribeTrades('BTC/USDT');
stopListenFn();
Subscribe to the websocket candle of the chosen symbol and time frame.
Once called you'll be able to listen to candle events for this symbol.
connect
method must be called before calling this one.
Valid time frame: '1m', '3m', '15m', '30m', '1h', '2h', '4h', '6h', '8h', '12h', '1d', '1w', '1M'
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeCandles('BTC/USDT', '1d');
ftxWS.on('candle-BTC/USDT-1d', candle => console.log(candle));
Unsubscribe from the candle websocket of the associated symbol. Once called no more events will be dispatched.
import { FtxWS } from '@iofate/ftx-ws';
const ftxWS = new FtxWS();
ftxWS.subscribeCandles('BTC/USDT', '1d');
const stopListenFn = ftxWS.on('candle-BTC/USDT-1d', candle => console.log(candle));
ftxWS.unsubscribeCandles('BTC/USDT', '1d');
stopListenFn();
Close the connection between you and KuCoin. You must unsubscribe from everything before calling this method!
Return a boolean which indicate if the socket is open or not.
Return a boolean which indicate if the socket is connecting or not.
Return the number of subscriptions.
Any merge request must follow conventional commits format.