Unsubscribe token in WebSocket is not working

APPU
APPU edited October 2023 in Java client
Hi,

I am trying to subscribe and unsubscribe tokens dynamically in the websocket. When I am unsubscribing, it doesn't unsubscribe. subscribe and unsubscribe based on the Boolean variable passed into the method along with instrument. The text file is still updating with old instrument values

Snippet of code (method to get live price and will store it in text file (BuyTickerStore.txt))
kiteLivePrice(String token, boolean subscribe) {
KiteTicker tickerProvider = tickerProviderInitialize();
ArrayList<Long> tokens = new ArrayList<>();
ArrayList<Long> tokensUnsubscribe = new ArrayList<>();
tokens.add(Long.valueOf(KiteAppConstants.NIFTYID));
if (!CommonUtils.isEmpty(token)) {
if (subscribe) {
tokens.add(Long.valueOf(token));
} else
tokensUnsubscribe.add(Long.valueOf(token));

}

tickerProvider.setOnConnectedListener(new OnConnect() {
@Override
public void onConnected() {
/**
* Subscribe ticks for token. By default, all tokens are subscribed for
* modeQuote.
*/
tickerProvider.unsubscribe(tokensUnsubscribe);
tickerProvider.subscribe(tokens);
tickerProvider.setMode(tokens, KiteTicker.modeFull);
}
});

tickerProvider.setOnTickerArrivalListener(new OnTicks() {
public void onTicks(ArrayList<Tick> ticks) {
for (Tick tick : ticks) {
PrintWriter writer = null;
try {
if (Long.valueOf(KiteAppConstants.NIFTYID).equals(tick.getInstrumentToken())) {
writer = new PrintWriter(KiteAppConstants.DATASTOREPATH + "TickerStore.txt");
writer.print(tick.getLastTradedPrice());
writer.close();
} else {
if(subscribe) {
writer = new PrintWriter(KiteAppConstants.DATASTOREPATH + "BuyTickerStore.txt");
writer.print(tick.getLastTradedPrice());
writer.close();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}
});
Sample test executed:
 kiteLivePrice("11308290",true);
Thread.sleep(30000);
kiteLivePrice("11308290",false);
Thread.sleep(1000);
clearBuyTickerStore(); /* Clearing text file **/


kiteLivePrice("11307522",true);
Thread.sleep(30000);
kiteLivePrice("11307522",false);
Thread.sleep(1000);
clearBuyOrderPlacedOrder();
Sign In or Register to comment.