javakiteconnect v1.4.3 with Websocket reconnect feature

sujith
sujith edited July 2017 in Java client
javakiteconnect is updated with auto reconnection mechanism, check out tickerUsage in examples to know more about usage.
It also includes new POJO for getIndicesQuote.
  • pranksterguru
    is this below code responsible for configuring autoreconnect? does it handle both local(client side) and remote(zerodha) disconnection?


    tickerProvider.setTryReconnection(true);
    //minimum value must be 5 for time interval for reconnection
    tickerProvider.setTimeIntervalForReconnection(5);
    //set number to times ticker can try reconnection, for infinite retries use -1
    tickerProvider.setMaxRetries(10);


  • sujith
    Hi,
    All handling is on client side.
    There is a timer task running every second to check last tick arrived time. If difference between current time and last tick arrived time is more than user specified time interval then it will reconnect.
    You can also specify number of times it can retry.
  • sujith
    @pranksterguru,
    Yes, those are the three magical lines ;)
  • pranksterguru
    thanks. i am not sure if you tested this situation.
    does it check ticker arrival of all the stocks? or distinct to each stock

    lets say there is a very less active stock i subscribed along with many other active stocks. the less active stock may not have ticker every second.. and if i had given tickerProvider.setTimeIntervalForReconnection(5);
    and i do not get ticker for 5 seconds for less active stock.. does the api think its disconnected and try to reconnect again?

  • sujith
    @pranksterguru,
    Websocket underlying protocol includes a ping pong mechanism and Kite ticker conforms to same protocol and it sends heartbeat ping every second if it is connected.
  • pivottrading.net
    Hi sujith,

    I was playing with the Java Streaming Code.
    I found that today on 26-04-2017 (10:44 am), Only the MCX instruments are getting streaming prices.
    I subscribed to NSE and NFO tokens, but not getting any tick price updates.

    Can you please check if any problem.
  • sujith
    Hi Saurabh,
    We use the same streaming websocket on Kite Web also. We haven't received any complaints. We were monitoring some things from the morning and we didn't come across this issue. Can you mention for which scrip you tried?
  • pivottrading.net
    Sujith,
    I tried for the below
    KiteConnectUtility.addTokens(53394951); //NICKEL APR
    KiteConnectUtility.addTokens(53385735); //CURDE MAY
    KiteConnectUtility.addTokens(5633); //ACC
    KiteConnectUtility.addTokens(738561); //RELIANCE
    KiteConnectUtility.addTokens(12668930); //NIFTY MAY 17 FUT
    //KiteConnectUtility.addTokens(340481); //HDFC
    //KiteConnectUtility.addTokens(2939649); //LT
    //KiteConnectUtility.addTokens(895745); //TATASTEEL

    First 2 worked properly, others didnt
  • pivottrading.net
    pivottrading.net edited April 2017
    Here is my code for KiteConnectUtilityClass


    package utility;

    import com.neovisionaries.ws.client.WebSocketException;
    import com.rainmatter.kiteconnect.KiteConnect;
    import com.rainmatter.kitehttp.exceptions.KiteException;
    import com.rainmatter.models.Tick;
    import com.rainmatter.ticker.KiteTicker;
    import com.rainmatter.ticker.OnConnect;
    import com.rainmatter.ticker.OnDisconnect;
    import com.rainmatter.ticker.OnTick;
    import data.StreamingPriceMap;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;

    /**
    * Created with IntelliJ IDEA.
    * User: LUCKIE
    * Date: 4/23/17
    * Time: 5:08 PM
    * To change this template use File | Settings | File Templates.
    */
    public class KiteConnectUtility {
    private static ArrayList<Long> tokens = new ArrayList<Long>();
    private static StreamingPriceMap priceMap;

    public static void addTokens(long tokenValue){
    tokens.add(tokenValue);
    }

    public static void removeToke(long tokenValue){
    tokens.remove(tokenValue);
    }

    public static void setPriceMap(StreamingPriceMap priceMap) {
    KiteConnectUtility.priceMap = priceMap;
    }

    public static void startTicker(KiteConnect kiteconnect) throws IOException, WebSocketException, KiteException {
    /** To get live price use com.rainmatter.ticker 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.*/
    final KiteTicker tickerProvider = new KiteTicker(kiteconnect);
    tickerProvider.setOnConnectedListener(new OnConnect() {
    @Override
    public void onConnected() {
    try {
    /** Subscribe ticks for token.
    * By default, all tokens are subscribed for modeQuote.
    * */
    tickerProvider.subscribe(tokens);
    } catch (IOException e) {
    e.printStackTrace();
    } catch (WebSocketException e) {
    e.printStackTrace();
    } catch (KiteException e) {
    e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }
    }
    });

    tickerProvider.setOnDisconnectedListener(new OnDisconnect() {
    @Override
    public void onDisconnected() {
    System.out.println("Disconnected.");
    // your code goes here
    }
    });

    tickerProvider.setOnTickerArrivalListener(new OnTick() {
    @Override
    public void onTick(ArrayList<Tick> ticks) {
    for(Tick t : ticks){
    priceMap.updatePrice(String.valueOf(t.getToken()), t);
    }
    System.out.println("No of Ticks: " + ticks.size());
    }
    });


    /** for reconnection of ticker when there is abrupt network disconnection, use the following code
    by default tryReconnection is set to false */
    tickerProvider.setTryReconnection(true);
    //minimum value must be 5 for time interval for reconnection
    tickerProvider.setTimeIntervalForReconnection(50);
    //set number to times ticker can try reconnection, for infinite retries use -1
    tickerProvider.setMaxRetries(-1);


    /** connects to com.rainmatter.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.rainmatter.ticker, close websocket connection.
    //tickerProvider.disconnect();
    }
    }
  • pivottrading.net
    Sujith,

    Its (Streaming) not working even today. Can you confirm it is working in your machine by using the same instrument tokens.

    Thanks,
    Sourabh.
  • sujith
    Hi Saurabh,
    Above code looks fine and I just tested websockets with above mentioned tokens and i am getting data. Maybe some other part of code is affecting.
  • pivottrading.net
    Ok. Thanks. I will investigate at my end.
  • pivottrading.net
    Found the probelm in my code. Thanks sujith. Amazing API.
  • Raja
    Hello,

    I am using the service of Kite connect. I need all the instrument list of nifty which contain the information of all the nifty stocks with open high low in just one request to server. Now I just get the only one quotas using below example, https://api.kite.trade/instruments/NSE/INFY?api_key=xxx&access_token=yay, kindly suggest
  • sachet_singh
    I ran this utility, and subscribed for 408065 (infy), full quote mode. I get only HeartBeat messages, and no quote messages. Any idea what could be wrong. thanks

    the websocket call goes fine

    wss://websocket.kite.trade/?api_key=My_Key&user_id=My_User&public_token=MyToken

  • sujith
    Hi @sachet_singh,
    Please create a new thread for the issue with code or you can search for webscoket related queries that are already answered.
Sign In or Register to comment.