I see this issue happening in my Java application when I subscribe to the ticker only for 5-6 instruments via Java Client
com.zerodhatech.kiteconnect kiteconnect 3.4.0
I have added logs in ticker onConnected/onDisconnected listeners and flow of logs is as below
INFO ZerodhaKite - Z ticker Connected for PZxxxx INFO ZerodhaKite - Z ticker Disconnected for PZxxxx ERROR ZerodhaKite - Z ticker KiteException for PZxxxx com.zerodhatech.kiteconnect.kitehttp.exceptions.KiteException at com.zerodhatech.ticker.KiteTicker.subscribe(KiteTicker.java:400) at com.zerodhatech.ticker.KiteTicker$4.onConnected(KiteTicker.java:663) at com.zerodhatech.ticker.KiteTicker$3.onConnected(KiteTicker.java:243) at com.neovisionaries.ws.client.ListenerManager.callOnConnected(ListenerManager.java:189) at com.neovisionaries.ws.client.WebSocket.callOnConnectedIfNotYet(WebSocket.java:3457) at com.neovisionaries.ws.client.WebSocket.onWritingThreadStarted(WebSocket.java:3427) at com.neovisionaries.ws.client.WritingThread.main(WritingThread.java:84) at com.neovisionaries.ws.client.WritingThread.runMain(WritingThread.java:55) at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
I perform m_tickerProvider.connect(); exactly at 9:16 AM and subscribe happens when the ticker gets connected inside the connection listener as below
m_tickerProvider.setOnConnectedListener(new OnConnect() { @Override public void onConnected() { /** Subscribe ticks for token. * By default, all tokens are subscribed for modeQuote. * */ logMsg("Z ticker Connected for " + getUserId()); m_tickerProvider.subscribe(tokens); m_tickerProvider.setMode(tokens, KiteTicker.modeFull); } });
Please help me understand the issue. I have only 1 ticker instance created (I know max limit is 3 websocket connections per API key) This is happening every day and the only way out is to restart my program multiple times and there is chance that once it connects properly after many attempts.
You shouldn't block the thread that is retrieving data. It should be always available to receive ticks. If you are doing some operation inside onTick, then you should move everything to a different thread.
And Yes, I have already taken care of not doing any work in the ticker thread. My code snippet is below to show the approach
protected BlockingQueue m_tickQueue= new LinkedBlockingDeque<>();
m_tickerProvider.setOnTickerArrivalListener(new OnTicks() { @Override public void onTicks(ArrayList ticks) { if (ticks.size() > 0) { m_tickQueue.addAll(ticks); } } });
Still I see this issue happen in the mornings especially. Tried to connect before the market (say 9:10 AM) and also once market starts (9:16 AM) Even then this fails.
Please let me know why that KiteException is thrown for when subscribe is called?
I will print the msg attribute in my code and share it here again tomorrow.
But @sujith can you please clarify the second question "Are you doing any other API call, in secondary thread?"
After I add the tick to a queue, another thread will read this queue, pick up the Tick from the Q and based on its value it will place some order via API. Any issue you see there?
Here is the stack trace having the message of the exception.
I see it says ticker is disconnected when we are trying to subscribe.
This is the real problem as to why the ticker is toggling so heavily between connected and disconnected states.
This issue happens wildly in the morning and later on after a couple of process restarts, maybe after 9:30 or so, it subsides
- Error after onConnected is invoked with msg : ticker is not connected and code 504
2023-12-08 09:09:27,222 +0530 [WritingThread] ERROR (LogUtil.java:22) - Z
ticker KiteException for GP****
com.zerodhatech.kiteconnect.kitehttp.exceptions.KiteException
at com.zerodhatech.ticker.KiteTicker.subscribe(KiteTicker.java:400
)
at com.zerodhatech.ticker.KiteTicker$4.onConnected(KiteTicker.java
:663)
at com.zerodhatech.ticker.KiteTicker$3.onConnected(KiteTicker.java
:243)
at com.neovisionaries.ws.client.ListenerManager.callOnConnected(Li
stenerManager.java:189)
at com.neovisionaries.ws.client.WebSocket.callOnConnectedIfNotYet(
WebSocket.java:3457)
at com.neovisionaries.ws.client.WebSocket.onWritingThreadStarted(W
ebSocket.java:3427)
at com.neovisionaries.ws.client.WritingThread.main(WritingThread.j
ava:84)
at com.neovisionaries.ws.client.WritingThread.runMain(WritingThrea
d.java:55)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThrea
d.java:45)
Please help me move forward @sujith
This continuous connection and disconnection is killing my program.
Every instance of KiteException has a message attribute like this. Can you print the same in the catch block and paste the message here? If you are placing order then it could be the exception from the http API as well.
Hi @sujith I am calling "subscribe" inside the OnConnectedListener like in the code below. I understand this is the same flow that is mentioned in the examples as well.
===== My analysis on the code flow ===== 1/ Call m_tickerProvider.connect(); 2/ KiteTicker connects and invokes the OnConnectedListener 3/Inside the OnConnectedListener, call "subscribe"
Between point 2/ and 3/ above, ticker has been "disconnected" duw to which when subscribe is being tried, since ws.isOpen() is FALSE, we get this exception with code 504
The real issue is why the ticker which was connected for an instant, suddenly disconnected.
m_tickerProvider.setOnConnectedListener(new OnConnect() { @Override public void onConnected() { /** Subscribe ticks for token. * By default, all tokens are subscribed for modeQuote. * */ try{ logAndSendMsg(LOGGER, TelegramUtil2.getAdminMessageChannel(), "Z ticker Connected for " + getUserId()); m_tickerProvider.subscribe(tokens); m_tickerProvider.setMode(tokens, KiteTicker.modeFull); }catch (Throwable t){ if(t instanceof KiteException){ LOGGER.error("Error after onConnected is invoked with msg : " + ((KiteException)t).message + " and code " + ((KiteException)t).code); } } } });
Kite Ticker doesn't have any issue. It is used by thousands of production apps. You need to check code at your end. Add logs and also make sure to enable Kite Connect debug logs as well.
Of course KiteConnnect is working well for hundreds of customers. I am one of them as well.
Since I am following all the steps recommended in the documentation, I am requesting you to take a look at the issue faced here.
My code implementation is not even involved here.
If there is something wrong done during subscribing or connecting to a ticker, it might help other developers as as well.
So please, take a look below at the flow and help me understand the right usage.
1/Broker login performed 8:46 am
2/ Connect ticker at 8:55 am
3/ ticker Onconnect listener invokes subscribe after 8:55 am
4/ series of connect and disconnect logs happening from 9:15 am until around 9:30am
5/ simply restart the process and it works fine.
This is the day to day behaviour.
I need your help to know what is deviating from the recommended flow.
@sathyatej, Kite Connect is purely an execution platform. We don't provide one to one support or coding support. We are all developers like you, we work on our projects and take some time out to help community members. Kite Ticker doesn't have any issue. You need to debug your code.
Thanks for responding!
And Yes, I have already taken care of not doing any work in the ticker thread.
My code snippet is below to show the approach
protected BlockingQueue m_tickQueue= new LinkedBlockingDeque<>();
m_tickerProvider.setOnTickerArrivalListener(new OnTicks() {
@Override
public void onTicks(ArrayList ticks) {
if (ticks.size() > 0) {
m_tickQueue.addAll(ticks);
}
}
});
Still I see this issue happen in the mornings especially.
Tried to connect before the market (say 9:10 AM) and also once market starts (9:16 AM)
Even then this fails.
Please let me know why that KiteException is thrown for when subscribe is called?
Are you doing any other API call, in secondary thread?
But @sujith can you please clarify the second question
"Are you doing any other API call, in secondary thread?"
After I add the tick to a queue, another thread will read this queue, pick up the Tick from the Q and based on its value it will place some order via API.
Any issue you see there?
Here is the stack trace having the message of the exception.
I see it says ticker is disconnected when we are trying to subscribe.
This is the real problem as to why the ticker is toggling so heavily between connected and disconnected states.
This issue happens wildly in the morning and later on after a couple of process restarts, maybe after 9:30 or so, it subsides
- Error after onConnected is invoked with msg : ticker is not connected and code 504
2023-12-08 09:09:27,222 +0530 [WritingThread] ERROR (LogUtil.java:22) - Z
ticker KiteException for GP****
com.zerodhatech.kiteconnect.kitehttp.exceptions.KiteException
at com.zerodhatech.ticker.KiteTicker.subscribe(KiteTicker.java:400
)
at com.zerodhatech.ticker.KiteTicker$4.onConnected(KiteTicker.java
:663)
at com.zerodhatech.ticker.KiteTicker$3.onConnected(KiteTicker.java
:243)
at com.neovisionaries.ws.client.ListenerManager.callOnConnected(Li
stenerManager.java:189)
at com.neovisionaries.ws.client.WebSocket.callOnConnectedIfNotYet(
WebSocket.java:3457)
at com.neovisionaries.ws.client.WebSocket.onWritingThreadStarted(W
ebSocket.java:3427)
at com.neovisionaries.ws.client.WritingThread.main(WritingThread.j
ava:84)
at com.neovisionaries.ws.client.WritingThread.runMain(WritingThrea
d.java:55)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThrea
d.java:45)
Please help me move forward @sujith
This continuous connection and disconnection is killing my program.
Can you print the same in the catch block and paste the message here? If you are placing order then it could be the exception from the http API as well.
"Error after onConnected is invoked with msg : ticker is not connected and code 504"
The piece of code having the try-catch block which printed that log msg is below
try{
logAndSendMsg(LOGGER, TelegramUtil2.getAdminMessageChannel(), "Z ticker Connected for " + getUserId());
m_tickerProvider.subscribe(tokens);
m_tickerProvider.setMode(tokens, KiteTicker.modeFull);
}catch (Throwable t){
if(t instanceof KiteException){
LOGGER.error("Error after onConnected is invoked with msg : " + ((KiteException)t).message + " and code " + ((KiteException)t).code);
}
}
I am calling "subscribe" inside the OnConnectedListener like in the code below.
I understand this is the same flow that is mentioned in the examples as well.
=====
My analysis on the code flow
=====
1/ Call m_tickerProvider.connect();
2/ KiteTicker connects and invokes the OnConnectedListener
3/Inside the OnConnectedListener, call "subscribe"
Between point 2/ and 3/ above, ticker has been "disconnected" duw to which when subscribe is being tried, since ws.isOpen() is FALSE, we get this exception with code 504
The real issue is why the ticker which was connected for an instant, suddenly disconnected.
m_tickerProvider.setOnConnectedListener(new OnConnect() {
@Override
public void onConnected() {
/** Subscribe ticks for token.
* By default, all tokens are subscribed for modeQuote.
* */
try{
logAndSendMsg(LOGGER, TelegramUtil2.getAdminMessageChannel(), "Z ticker Connected for " + getUserId());
m_tickerProvider.subscribe(tokens);
m_tickerProvider.setMode(tokens, KiteTicker.modeFull);
}catch (Throwable t){
if(t instanceof KiteException){
LOGGER.error("Error after onConnected is invoked with msg : " + ((KiteException)t).message + " and code " + ((KiteException)t).code);
}
}
}
});
Of course KiteConnnect is working well for hundreds of customers. I am one of them as well.
Since I am following all the steps recommended in the documentation, I am requesting you to take a look at the issue faced here.
My code implementation is not even involved here.
If there is something wrong done during subscribing or connecting to a ticker, it might help other developers as as well.
So please, take a look below at the flow and help me understand the right usage.
1/Broker login performed 8:46 am
2/ Connect ticker at 8:55 am
3/ ticker Onconnect listener invokes subscribe after 8:55 am
4/ series of connect and disconnect logs happening from 9:15 am until around 9:30am
5/ simply restart the process and it works fine.
This is the day to day behaviour.
I need your help to know what is deviating from the recommended flow.
Thanks again @sujith for your responses
Can you respond to my query above?
Even today I am seeing constant connection and disconnection happening across my accounts at random times.
I request you to atleast treat this as a case for investigation from your side as well.
I can privately share the account IDs which are having an issue.
We trust zerodha for two main things, it's reliability and it's developer friendly approach.
Hope we can work to identify the root cause.
Kite Connect is purely an execution platform. We don't provide one to one support or coding support.
We are all developers like you, we work on our projects and take some time out to help community members. Kite Ticker doesn't have any issue. You need to debug your code.
Believe me it isn't.
Btw the same code works with sharekhan as my code is broker generic.
My intention was to investigate constant disconnections.
Anyways, I shall drop this thread now.
Thank you.