Compatibility Issue with WebSocket v2 in Marketfeed Module

I’m currently working on implementing near real-time order information in my app using the websocket v2 through the Marketfeed module. However, I encountered an issue while trying to initiate the stream—specifically, I faced difficulties maintaining the connection in an open state.

After investigating, I found that the way the websocket connection is closed or reopened in the Marketfeed code isn’t compatible with the latest websockets library version, 14.1. While reviewing the module, I noticed that the documentation mentions compatibility with versions higher than 11.1.0, but it seems the current implementation doesn’t fully support 14.1.

To resolve this, I downgraded the websockets library to an earlier version, and the program worked as expected. However, I believe this issue should be addressed, either by:

  1. Updating the Marketfeed code for compatibility with newer websockets versions (like 14.1), or
  2. Clearly communicating to users that they should use a specific compatible version of the library.

Here’s an example of the change I made to get it working with WebSocket 14.1:

Original Code in Marketfeed.py:

python

async def connect(self):
    """Initiates the connection to the Websockets."""
    if not self.ws or self.ws.closed:

Updated Code for WebSocket 14.1 Compatibility:

python

async def connect(self):
    """Initiates the connection to the Websockets."""
    if not self.ws or self.ws.state == websockets.protocol.State.CLOSED:

I hope this helps others who might face similar issues. It would be great if the maintainers could address this in future updates to ensure compatibility with the latest library versions.

2 Likes

Hi @Santhosh_Murali ,
I am getting the error - TypeError: a coroutine was expected, got None. Could you help?

Hello @Santhosh_Murali

Welcome to MadeForTrade community!

Thank you for highlighting this. Will change the library accordingly. If you are okay with contributing, you can also directly raise PR here: GitHub - dhan-oss/DhanHQ-py: The official Python client for communicating with the Dhan API.

1 Like

@JJ_24
if you creating a async function, you will have to assign it to task

a1 = loop.create_task(get())
loop.run_until_complete(a1)