Navigation

    SmartAPI Forum
    • Register
    • Login
    • Search
    • Categories
    • Popular
    • Groups
    • FAQs
    • API Docs

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

    Python SDK
    0
    14
    171
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      projectSB last edited by projectSB

      @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

      A K A 3 Replies Last reply Reply Quote 1
      • N
        nagabhushant last edited by

        Hi,

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

        P 1 Reply Last reply Reply Quote 0
        • P
          projectSB @nagabhushant last edited by

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

          A 1 Reply Last reply Reply Quote 0
          • A
            af @projectSB last edited by

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

            P 1 Reply Last reply Reply Quote 0
            • P
              projectSB @af last edited by

              @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()

              1 Reply Last reply Reply Quote 1
              • A
                ajileshparolla @projectSB last edited by

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

                P 1 Reply Last reply Reply Quote 0
                • P
                  projectSB @ajileshparolla last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • K
                    kishan9907 @projectSB last edited by

                    @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
                    
                    P 1 Reply Last reply Reply Quote 1
                    • A
                      af @projectSB last edited by

                      @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

                      1 Reply Last reply Reply Quote 0
                      • J
                        J88913 last edited by

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

                        1 Reply Last reply Reply Quote 0
                        • B
                          Bhuva008 last edited by

                          @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

                          1 Reply Last reply Reply Quote 0
                          • B
                            Bhuva008 last edited by

                            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)

                            P 1 Reply Last reply Reply Quote 1
                            • P
                              projectSB @kishan9907 last edited by

                              @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!

                              1 Reply Last reply Reply Quote 0
                              • P
                                projectSB @Bhuva008 last edited by

                                @Bhuva008 Thanks! will try this as well

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post