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