Last active
March 20, 2023 13:05
-
-
Save tiagosiebler/e39126ef0d63984d156f0e30c8552a3e to your computer and use it in GitHub Desktop.
Revisions
-
tiagosiebler revised this gist
Mar 20, 2023 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ // import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; // or import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api'; const logger = { ...DefaultLogger, @@ -13,7 +13,7 @@ const wsClient = new WebsocketClient( // key: key, // secret: secret, market: 'contractUSDT', // testnet: true, }, logger ); -
tiagosiebler created this gist
Mar 20, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,86 @@ import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from '../src'; // or // import { DefaultLogger, WS_KEY_MAP, WebsocketClient } from 'bybit-api'; const logger = { ...DefaultLogger, silly: (...params) => console.log(new Date(), 'silly', ...params), }; const wsClient = new WebsocketClient( { // key: key, // secret: secret, market: 'contractUSDT', testnet: true, }, logger ); wsClient.on('update', (data) => { console.info( new Date(), '----> DATA Received ', data?.topic // JSON.stringify(data) ); }); wsClient.on('open', (data) => { console.log(new Date(), 'connection opened open:', data.wsKey); }); wsClient.on('response', (data) => { console.log(new Date(), 'log response: ', JSON.stringify(data, null, 2)); }); wsClient.on('reconnect', ({ wsKey }) => { console.log(new Date(), 'ws automatically reconnecting.... ', wsKey); }); wsClient.on('reconnected', (data) => { console.log(new Date(), 'ws has reconnected ', data?.wsKey); }); wsClient.on('error', (data) => { console.error(new Date(), 'ws ERROR: ', data); }); function getTopicsForSymbol(symbol: string): string[] { return [ `orderbook.1.${symbol}`, `orderbook.50.${symbol}`, `publicTrade.${symbol}`, `tickers.${symbol}`, ]; } wsClient.subscribe(getTopicsForSymbol('BNBUSDT')); const publicUsdtContractTopics = wsClient .getWsStore() .getTopics(WS_KEY_MAP.contractUSDTPublic); console.log( new Date(), 'Checking active topics - expect 4:', publicUsdtContractTopics ); const SWITCH_SYMBOL_AFTER_SECONDS = 5; // To unsubscribe from topics (after a 5 second delay, in this example): setTimeout(() => { console.log(new Date(), 'Executing unsub from BNB and sub to BTC:'); wsClient.unsubscribe(getTopicsForSymbol('BNBUSDT')); wsClient.subscribe(getTopicsForSymbol('BTCUSDT')); }, SWITCH_SYMBOL_AFTER_SECONDS * 1000); // Topics are tracked per websocket type // Get a list of subscribed topics (e.g. for public v3 spot topics) (after a 5 second delay) setTimeout(() => { const publicUsdtContractTopics = wsClient .getWsStore() .getTopics(WS_KEY_MAP.contractUSDTPublic); console.log( new Date(), 'Checking active topics - expect 0:', publicUsdtContractTopics ); }, (SWITCH_SYMBOL_AFTER_SECONDS + 1) * 1000);