Last active
March 20, 2023 13:05
-
-
Save tiagosiebler/e39126ef0d63984d156f0e30c8552a3e to your computer and use it in GitHub Desktop.
unsub from BNB topics and sub to BTC events after delay
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 characters
// 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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment