Websocket2.0 just stops after running for 1 min without any errors etc.


  • @admin - Please look into this. I'm trying to run the WebSocket 2.0. The App starts fine, tokens were subscribed, and I start getting feed properly.
    But after around 30 seconds, I see a message Attempting to Reconnect.. feed still goes fine. After, this around 20-30 seconds later the App just exits, WITHOUT any errors. This is becoming a real pain!

    Here is the log:
    2023-08-30 14:32:42,571 [{'exchangeType': 2, 'tokens': ['35048']}]
    2023-08-30 14:32:42,714 Websocket connected
    2023-08-30 14:32:42,714 Connection opened, subscriptions done
    2023-08-30 14:33:02,774 error from callback <bound method SmartWebSocketV2._on_pong of <SmartApi.smartWebSocketV2.SmartWebSocketV2 object at 0x000001AB63A9FFD0>>: byte indices must be integers or slices, not str
    2023-08-30 14:33:02,962 Websocket connected
    2023-08-30 14:33:02,962 Connection opened, subscriptions done
    2023-08-30 14:33:23,056 error from callback <bound method SmartWebSocketV2._on_pong of <SmartApi.smartWebSocketV2.SmartWebSocketV2 object at 0x000001AB63A9FFD0>>: byte indices must be integers or slices, not str
    2023-08-30 14:33:23,103 error from callback <bound method SmartWebSocketV2._on_close of <SmartApi.smartWebSocketV2.SmartWebSocketV2 object at 0x000001AB63A9FFD0>>: SmartWebSocketV2._on_close() takes 2 positional arguments but 4 were given
    2023-08-30 14:33:23,103 error from callback <bound method SmartWebSocketV2._on_close of <SmartApi.smartWebSocketV2.SmartWebSocketV2 object at 0x000001AB63A9FFD0>>: SmartWebSocketV2._on_close() takes 2 positional arguments but 4 were given


  • Hi,

    Are you using ws://smartapisocket.angelone.in/smart-stream for websocket 2.0 connection?


  • @nagabhushant Well, I'm using WebSocket 2.0 from inside the Python SDK, as per the guidelines provided in the Github page.


  • @projectSB can you share the code i am not able to find the exact version


  • @af I'm using the Websocket version 2.0 code as provided in this Github link:

    https://github.com/angel-one/smartapi-python

    This portion:

    ####### Websocket V2 sample code #######

    from SmartApi.smartWebSocketV2 import SmartWebSocketV2
    from logzero import logger

    AUTH_TOKEN = "Your Auth_Token"
    API_KEY = "Your Api_Key"
    CLIENT_CODE = "Your Client Code"
    FEED_TOKEN = "Your Feed_Token"
    correlation_id = "abc123"
    action = 1
    mode = 1
    token_list = [
    {
    "exchangeType": 1,
    "tokens": ["26009"]
    }
    ]
    sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN)

    def on_data(wsapp, message):
    logger.info("Ticks: {}".format(message))
    # close_connection()

    def on_open(wsapp):
    logger.info("on open")
    sws.subscribe(correlation_id, mode, token_list)

    def on_error(wsapp, error):
    logger.error(error)

    def on_close(wsapp):
    logger.info("Close")

    def close_connection():
    sws.close_connection()

    Assign the callbacks.

    sws.on_open = on_open
    sws.on_data = on_data
    sws.on_error = on_error
    sws.on_close = on_close

    sws.connect()


  • @projectSB Did you find any solution for this? I am having the same issue.


  • @ajileshparolla Nope! no solutions so far 😞
    no admins replied as well


  • @projectSB see if this helps, I've added below code when I got a similar error

    def on_data(wsapp, message):
            if message != b'\x00':
                #process data
    

  • @projectSB i have been getting same problem. but i got the solution. go to your on
    smartWebscketV2.py and find this " def on_close(self, wsapp):
    pass
    " on your side the argument would be 4 in there make it like this. and thats it. it will work


  • Watch the video and download the code to get the live data into Excel
    Youtube Video


  • @admin
    yes facing same , connection is getting disconnected
    in first 30 sec,
    Attempting to resubscribe/reconnect...
    next 30 sec,
    Connection closed
    Connection closed
    Connection closed


  • Hello looks like i found solution !

    use try: expect : in you on_data definition , looks like sometime in socket it is not able to decode stream and get close rather then printing error.

    def on_data(wsapp, msg):
    #print("Ticks: {}".format(msg))
    try:
    LIVE_FEED_JSON[msg['token']] = {'token' : msg['token'],'ltp' : msg['last_traded_price']/100 , 'timestamp' : datetime.fromtimestamp(msg['exchange_timestamp']/1000).isoformat()}
    print(LIVE_FEED_JSON)
    except Exception as e:
    print(e)


  • @kishan9907 said in Websocket2.0 just stops after running for 1 min without any errors etc.:

    if message != b'\x00':

    Thanks! I have implemented this into my code. Hope this works!


  • @Bhuva008 Thanks! will try this as well