Java Websocket client not giving any output

wealthfromtrading
wealthfromtrading edited August 2018 in Java client
I am facing a problem while trying to get the real time quotes. I am using websocket with java library. Here is the code:
User user =  kiteConnect.generateSession(requestToken, AppConfig.apiSecret);
kiteConnect.setAccessToken(user.accessToken);
kiteConnect.setPublicToken(user.publicToken);

ArrayList<Long> tokens = new ArrayList<>();
tokens.add((424961l));
tokens.add(3861249l);
tokens.add(2585345l);



new WebSocketConnection().tickerUsage(kiteConnect, tokens);

this tickerUsage method is nothing but copied and pasted from the Examples of the java library with a few additional log statements. Here is it for quick reference:
/** Demonstrates com.zerodhatech.ticker connection, subcribing for instruments, unsubscribing for instruments, set mode of tick data, com.zerodhatech.ticker disconnection*/
public void tickerUsage(KiteConnect kiteConnect, ArrayList<Long> tokens) throws IOException, WebSocketException, KiteException {
/** To get live price use websocket connection.
* It is recommended to use only one websocket connection at any point of time and make sure you stop connection, once user goes out of app.
* custom url points to new endpoint which can be used till complete Kite Connect 3 migration is done. */
final KiteTicker tickerProvider = new KiteTicker(kiteConnect.getAccessToken(), kiteConnect.getApiKey());

tickerProvider.setOnConnectedListener(new OnConnect() {

public void onConnected() {
/** Subscribe ticks for token.
* By default, all tokens are subscribed for modeQuote.
* */
tickerProvider.subscribe(tokens);
tickerProvider.setMode(tokens, KiteTicker.modeFull);
System.out.println("onconnected");
}
});



tickerProvider.setOnDisconnectedListener(new OnDisconnect() {

public void onDisconnected() {
System.out.println("disconnected");

}


});

/** Set listener to get order updates.*/
tickerProvider.setOnOrderUpdateListener(new OnOrderUpdate() {
@Override
public void onOrderUpdate(Order order) {
System.out.println("order update "+order.orderId);
}
});


tickerProvider.setOnErrorListener(new OnError() {
@Override
public void onError(Exception exception) {
System.out.println(exception.getMessage());
}

@Override
public void onError(KiteException kiteException) {
System.out.println(kiteException.getMessage());
}
});




tickerProvider.setOnTickerArrivalListener(new OnTicks() {
@Override
public void onTicks(ArrayList<Tick> ticks) {
System.out.println("onTicks");
NumberFormat formatter = new DecimalFormat();
System.out.println("ticks size "+ticks.size());
if(ticks.size() > 0) {
System.out.println("last price "+ticks.get(0).getLastTradedPrice());
System.out.println("open interest "+formatter.format(ticks.get(0).getOi()));
System.out.println("day high OI "+formatter.format(ticks.get(0).getOpenInterestDayHigh()));
System.out.println("day low OI "+formatter.format(ticks.get(0).getOpenInterestDayLow()));
System.out.println("change "+formatter.format(ticks.get(0).getChange()));
System.out.println("tick timestamp "+ticks.get(0).getTickTimestamp());
System.out.println("tick timestamp date "+ticks.get(0).getTickTimestamp());
System.out.println("last traded time "+ticks.get(0).getLastTradedTime());
System.out.println(ticks.get(0).getMarketDepth().get("buy").size());
}
}
});

// Make sure this is called before calling connect.
tickerProvider.setTryReconnection(true);
//maximum retries and should be greater than 0
tickerProvider.setMaximumRetries(10);
//set maximum retry interval in seconds
tickerProvider.setMaximumRetryInterval(3);

/** connects to com.zerodhatech.com.zerodhatech.ticker server for getting live quotes*/
tickerProvider.connect();

/** You can check, if websocket connection is open or not using the following method.*/
boolean isConnected = tickerProvider.isConnectionOpen();
System.out.println(isConnected);

/** set mode is used to set mode in which you need tick for list of tokens.
* Ticker allows three modes, modeFull, modeQuote, modeLTP.
* For getting only last traded price, use modeLTP
* For getting last traded price, last traded quantity, average price, volume traded today, total sell quantity and total buy quantity, open, high, low, close, change, use modeQuote
* For getting all data with depth, use modeFull*/
tickerProvider.setMode(tokens, KiteTicker.modeLTP);

// Unsubscribe for a token.
tickerProvider.unsubscribe(tokens);

// After using com.zerodhatech.com.zerodhatech.ticker, close websocket connection.
tickerProvider.disconnect();
}
Now when i run the example, nothing is happening. Sometimes, i get a log statement saying "onconnected" (logged on onConnected method) but usually nothing.

Please advice
  • sujith
    Please go through the examples and try to understand before using it.
    The last two lines call unsubscribe and disconnect method of the Kite Ticker. You need to comment those two lines to get continuous streaming data.
  • wealthfromtrading
    thanks sujith. had figured this a couple of days back. thanks anyways for your time.
This discussion has been closed.