Dhanhq api v2.1.0 : WebSocket connection error: server rejected WebSocket connection: HTTP 429

Hi Team,

I am using dhanhq version 2.1.0 and getting error - WebSocket connection error: server rejected WebSocket connection: HTTP 429

I hav verified that my hand data api subscription is active . The python version is 3.10 and websockets version is 13.1 .

I tried everything but unable to resolve this.The datafeed was working fine in earlier version and suddenly stopped working and i am trying to upgrade to latest.

from dhanhq import DhanContext, MarketFeed
import asyncio
import nest_asyncio
nest_asyncio.apply()

client_id = "1000XXXXX"
access_token = """
iXumheETHFIgbhiBsx0WrsU0dIfGV_sorP2xfTsp4StDFfsu8ikkQnnFOj_n8kdcw
               """
instruments =[
    (MarketFeed.IDX, "13", MarketFeed.Ticker), #NIFTY
    (MarketFeed.IDX, "21", MarketFeed.Ticker), #INDIA VIX
    (MarketFeed.IDX, "25", MarketFeed.Ticker), #BANKNIFTY
    (MarketFeed.IDX, "27", MarketFeed.Ticker),  #FINNIFTY
    (MarketFeed.IDX, "442", MarketFeed.Ticker)  #MIDCPNIFTY
    ]

version = "v2"
dhan_context = DhanContext(client_id,access_token.strip())

async def fetchdata():
        print('Gen marketfeed v2 ..')
        data = MarketFeed(dhan_context, instruments, version)
        try:

            while True:
                print('Run forever:')
                data.run_forever()
                print('get data:')
                response = data.get_data()
                print('Response:')
                print((response))
                print('LTP : ',response['LTP'])

        except Exception as e:
            print('Exception:>>',e)

        # Close Connection
        print('Close Connection')
        await data.disconnect()

asyncio.run(fetchdata());

error snippet

Gen marketfeed v2 ..
Run forever:
Exception:>> server rejected WebSocket connection: HTTP 429
Close Connection
Connection closed!

Any help or suggestion is much appreciated.

Thanks

Hey @alok.nayak

What is the frequency here for you connecting to the websocket? Also, are you running multiple programs from your end as well?

Do note, there is an update on how websocket needs to be handled: here

1 Like

@Hardik ,

I am connecting only once to the websocket to fetch the datafeed.
No multiple programs are running from my end. Just trying the above code on pycharm editor.

Thanks

Hey @alok.nayak

Can you mail your IP details and Client ID on help@dhan.co with this thread, the team will check logs and assist you directly.

sent a mail to the support team .