Created
March 20, 2023 12:48
-
-
Save tiagosiebler/e369f01f69466580110ff704a4f4135b to your computer and use it in GitHub Desktop.
unsubscribe from topics on bybit sdk
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); | |
}); | |
const topicsToSub = [ | |
'orderbook.1.BNBUSDT', | |
'orderbook.50.BNBUSDT', | |
'publicTrade.BNBUSDT', | |
'tickers.BNBUSDT', | |
]; | |
wsClient.subscribe(topicsToSub); | |
const publicUsdtContractTopics = wsClient | |
.getWsStore() | |
.getTopics(WS_KEY_MAP.contractUSDTPublic); | |
console.log( | |
new Date(), | |
'Checking active topics - expect 4:', | |
publicUsdtContractTopics | |
); | |
// To unsubscribe from topics (after a 5 second delay, in this example): | |
setTimeout(() => { | |
console.log(new Date(), 'Executing unsubscribe:'); | |
wsClient.unsubscribe(topicsToSub); | |
}, 5 * 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 | |
); | |
}, 6 * 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment