Default WebSocket code doesnt work

soumyadeep
No output received on running this code on python. Please note that this is not market hours.
Also the infinite loop doesn’t work. The code has been taken from the documentation.
------------------------------------------code-------------------------------------
#!python

from kiteconnect import KiteConnect
from kiteconnect import WebSocket

api_key= xxx
public_token=yyy
user_id=zzz
access_token=ppp

kite = KiteConnect(api_key=api_key)
# Initialise. api_key, public_token, user_id are correct just as obtained.
kws = WebSocket(api_key, public_token, user_id)

# Callback for tick reception.
def on_tick(tick, ws):
print tick


# Callback for successful connection.
def on_connect(ws):
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
ws.subscribe([738561, 5633])

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_FULL, [738561])

#Reconnection command added by me as a backup measure.
def on_close(ws):
ws.reconnect()

# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()
  • soumyadeep
    soumyadeep edited July 2016
    @Kailash the default instructions on the python client documentation doesnt work. If someone simply executes what is written in the "Getting Started" section of WebSocket, it just doesnt stream data.

    Am I missing something? I want to have a live stream during market hours today. Can you please guide me?
    Thanks
  • Vivek
    @soumyadeep I just tested it with changing params and it seems to be working. Please check in market hours if it works and also would recommend implement python logging to log errors to catch any run time errors.
  • soumyadeep
    soumyadeep edited July 2016
    I am checking it now. It just doesnt give any outputs. May I know wha params did you change? If you could kindly give me a pice of working code, I can just test it on my Linux terminal.

    My Linux terminal just returns back just after I run the above script without giving me any output. There is no data or live streaming. I installed the python client only. Do I need to install anything extra for websocket?
  • Vivek
    Vivek edited July 2016
    @soumyadeep Here is the same example with logging. Please try this and check if you get any error log and also make sure params like api_key, public_token and user_id is set properly.
    #!python

    from kiteconnect import KiteConnect
    from kiteconnect import WebSocket

    import logging
    logging.basicConfig(level=logging.DEBUG)

    api_key= 'xyz'
    public_token='xyz'
    user_id='xyz'
    access_token='xyz'

    kite = KiteConnect(api_key=api_key)
    # Initialise. api_key, public_token, user_id are correct just as obtained.
    kws = WebSocket(api_key, public_token, user_id)

    # Callback for tick reception.
    def on_tick(tick, ws):
    logging.debug(tick)


    # Callback for successful connection.
    def on_connect(ws):
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe([738561, 5633])

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, [738561])

    #Reconnection command added by me as a backup measure.
    def on_close(ws):
    logging.debug("closed connection")
    ws.reconnect()

    # Assign the callbacks.
    kws.on_tick = on_tick
    kws.on_connect = on_connect
    # Infinite loop on the main thread. Nothing after this will run.
    # You have to use the pre-defined callbacks to manage subscriptions.
    kws.connect()
  • soumyadeep
    soumyadeep edited July 2016
    Hi Vivek,
    Thanks for the code. I ran it after putting the right values of api_key, public_token,user_id, access_token but the problem still exists.
    The terminal just comes back without streaming any data or showing any errors.
    There is no log output as well. I am using python 2.7. This is what I get in my terminal after I run the script three times:

    Soumya:~/Trading-Algo> python webeditsocket.py
    Soumya:~/Trading-Algo> python webeditsocket.py
    Soumya:~/Trading-Algo> python webeditsocket.py
    Soumya:~/Trading-Algo>

    Thanks
  • soumyadeep
    @vivek Any leads? I tried everything i could. No idea why is it happening.
  • Kailash
    @soumyadeep We tested this on different networks and systems and it works. This could be a network/firewall issue. Is it possible for you to maybe use WireShark to figure out what's happening over the wire?
  • Vivek
    @soumyadeep Have no clue what's going wrong at your end. Please implement on_error callback and check if its throwing any error.
  • soumyadeep
    Alright let me see what I can do. But given my code, I should get the data right?
    There is no "print" command in my script. But WebSocket should stream the data just after the command "kws.connect()". Please let me know if thats correct.
    I will see and keep you guys updated.
    Thanks
  • soumyadeep
    @vivek @Kailash SO after a lot of googling and trying out things, I figured out the problem- Connections to "wss" are rejected whereas if I change the "root" in init.py to _root = "ws://websocket.kite.trade/", live streaming starts immediately.
    I dont know how to bypass that and if that will hamper my security. Any suggestions?
  • Vivek
    @soumyadeep Please check whether SSL certificates are installed properly at your end, we will soon disable ws so wss is the right way to use.
  • soumyadeep
    soumyadeep edited July 2016
    UPDATE: I installed wireshark and when I use "ws", it shows me stream of websocket protocols with packet sizes. However when I use "wss", nothing comes in the wireshark.

    I can connect to other "wss" sites and get a response from them. But only wss://websocket.kite.trade is the problem.
  • Kailash
    @soumyadeep Do you have another machine or VM to test this on? This most definitely sounds like an issue with the CA trust store on your system. We do not have any other reports.
    https://www.sslshopper.com/ssl-checker.html#hostname=websocket.kite.trade
  • sayanriju
    Ok, so I seem to having the _exact_ same problem on my Ubuntu 14.04 box!

    What certificates should I be installing?
  • Kailash
    @sayanriju The certificate's route CA is Geotrust. It should be in your Python installation's trust store.

    We'll release a patch today with some SSL settings tweaked. Let's see if that'll help.
  • soumyadeep
    @Kailash I tried a different computer and it works there in windows. Couldnt try another Ubuntu though. Hope the patch works. Looking forward to it.
  • Kailash
    @soumyadeep @sayanriju We're releasing a patch tomorrow that should address this issue.
  • Kailash
    Kailash edited July 2016
    @soumyadeep @sayanriju We've released an update to the Python client (3.1.6). You can run a pip update: pip install kiteconnect -U

    You should pass the param disable_ssl_verification=True to the .connect() method. Please not that this is a workaround, not a fix.

    The issue is due to a bug in OpenSSL itself -- https://github.com/liris/websocket-client/issues/35

    The actual solution would be to update OpenSSL itself.
  • soumyadeep
    soumyadeep edited July 2016
    Thanks
This discussion has been closed.