Marketfeed API disconnecting sometimes

today i am not facing issue although , just checking if my code is correct or need any update?

version = “v2”.

Name: dhanhq
Version: 2.0.2
Summary: The official Python client for communicating with the DhanHQ API
Home-page: https://dhanhq.co/
Author: Dhan
Author-email: dhan-oss@dhan.co
License: MIT LICENSE
Location: c:\users\singhvi\appdata\local\programs\python\python38\lib\site-packages
Requires: pandas, pyOpenSSL, requests, websockets
Required-by:
PS C:\Dhan\Code>

This looks like the updated library, can you confirm the version of websockets as well?

Also, any changes that you did at your end?

no change at my end.
for websocket , i can not use 14.1 as its not compatible with 3.8 version of python.
does it make sense to use latest websocket 14.1 or 13.1? then i have to upgrade python version.

Name: websockets
Version: 13.1
Summary: An implementation of the WebSocket Protocol (RFC 6455 & 7692)
Home-page: GitHub - python-websockets/websockets: Library for building WebSocket servers and clients in Python
Author:
Author-email: Aymeric Augustin aymeric.augustin@m4x.org
License: BSD-3-Clause
Location: c:\users\singhvi\appdata\local\programs\python\python38\lib\site-packages
Requires:

No, it is not mandatory. It should work fine with 13.1 as well, but do let us know in case you face this disconnection issue again.

@Hardik getting same error again. please see below:

can you please provide me resolution. it disconnect and start from 1st instrument again and i can not proceed further with my algo.

WebSocket connection error: sent 1011 (internal error) keepalive ping timeout; no close frame received
c:/Dhan/Code/test_multi_symbol_feed.py:80: RuntimeWarning: coroutine ‘DhanFeed.disconnect’ was never awaited
data.disconnect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
c:/Dhan/Code/test_multi_symbol_feed.py:87: RuntimeWarning: coroutine ‘DhanFeed.disconnect’ was never awaited
data.disconnect()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Disconnected from WebSocket feed

@Hardik
error with trace:

2024-12-20 10:17:14,531 - ERROR - Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError(None, Close(code=<CloseCode.INTERNAL_ERROR: 1011>, reason=‘keepalive ping timeout’), None)>
Traceback (most recent call last):
File “C:\Users\singhvi\AppData\Local\Programs\Python\Python38\lib\site-packages\websockets\legacy\protocol.py”, line 964, in transfer_data
await asyncio.shield(self._put_message_waiter)
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

websockets.exceptions.ConnectionClosedError: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-20 10:17:14,533 - ERROR - Future exception was never retrieved
future: <Future finished exception=ConnectionClosedError(None, Close(code=<CloseCode.INTERNAL_ERROR: 1011>, reason=‘keepalive ping timeout’), None)>
Traceback (most recent call last):
File “C:\Users\singhvi\AppData\Local\Programs\Python\Python38\lib\site-packages\websockets\legacy\protocol.py”, line 964, in transfer_data
await asyncio.shield(self._put_message_waiter)
asyncio.exceptions.CancelledError

The above exception was the direct cause of the following exception:

i think this error occurs when i am trying to add response data in json file.
when i just print the response and dont do any manipulation on feed , it work fine.

it gave error as soon as we want to handle the data in csv or json.

so here is the question , how can i reference the live feed data if i can not store in json /excel etc ?

@Hardik please help!

@Hardik @Tradehull_Imran @Dhan
see i added logging and every minutes i am getting below error and i am simply printing marketfeed on terminal. thats it.

2024-12-23 12:17:31,688 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-23 12:18:23,097 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-23 12:19:11,067 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-23 12:20:01,491 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-23 12:20:52,288 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received
2024-12-23 12:21:42,850 - ERROR - Error processing item: sent 1011 (internal error) keepalive ping timeout; no close frame received

@Hardik if i remove time.sleep(0.2) , issue is resolved.

so below is my finding.

  1. data.run_forever() should be above while loop.
  2. if you add time sleep it give you timout 1101 error.
  3. better to store data in mongodb or some kafka topic for faster streamed data.
  4. if you store data in json or excel with some delay , you will get the error.
1 Like

Hello @virender_singh

Understood. This might not be because of storing in JSON or excel but because of the sleep that you are adding in between loop. If the connection is paused and then tried to resume, then the error will occur given the very nature of websockets.