(Resolved) WebSocket connection error: server rejected WebSocket connection: HTTP 429

D:\Dhan\Session3\Dhan codebase>py Dhan_websocket.py
This BOT Is Picking New File From Dhan
Watchlist changed. Reconnecting the feed…
Disconnected from WebSocket feed.
WebSocket connection error: server rejected WebSocket connection: HTTP 429
Reconnecting Again…

1 Like

This issue has been going on since 22/5 and have not received any solution yet. Dhan Team request you to look in to this issue.

Yes, I was just embarking on the journey of Algo Trading in Dhan, and was hit with this roadblock. BTW Dhan team is doing better then most of the brokers in explaining how Algo Trading works and providing in depth training on YouTube which is very much appreciated. I hope they can resolve this issue at the earliest.

Hello @Parth_Dabhi @sreeramtkd @nagaravikrishnadivi

Do note that whenever you get 429 error on the websocket or any API, it is because of too many requests. You can read more about why this error message can be seen on websocket here: Live Market Feed (WebSocket) vs. Market Quote (REST API) on DhanHQ

If your code is trying to reconnect to websocket multiple time just to fetch one single data point, you will be getting this error.

Thanks @Hardik for quick reply, I do see that error 429 is resolved. However now I’m facing another error 400 !! can you look into it.

2 Likes

This has to nothing with Rate limits, found that forcing in the algo version to v2 fixes this. Add the parameter on the data subscription. This should solve the issue.

version=“v2”
data = marketfeed.DhanFeed(client_id, access_token, instruments, version)

2 Likes

I am using dhanhq version 2.1.0 and getting the same error .

My code snippet

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

This is my error.

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

I was using API v1 version and tryying to switch to version 2. Any hep and suggestions would be really appreciated.

hello Parth, am also facing the same issue, would love your suggestion or help, if you have found any solution to this error.