dydxprotocol / dydx-v3-python

Python client for dYdX (API v3)
Apache License 2.0
304 stars 174 forks source link

Websocket disconnect 1006 error in Python 3.9 own implementation #194

Open dadastake opened 1 year ago

dadastake commented 1 year ago

Hello everyone,

I realise that this might not be exactly up for discussion here, but I will still share it since you've also built https://github.com/dydxprotocol/dydx-v3-python/blob/master/examples/websockets.py

I am building our own implementation from scratch of a websocket client. I can successfully send subscribe and unsubscribe messages for the orderbook and last trades feeds. Additionally I send a ping every 10 seconds and successfully receive a pong message back.

However after about 2 minutes of connection, the connection dies and when websocket.recv() is called, I receive the following error: websockets.exceptions.ConnectionClosedError: code = 1006 (connection closed abnormally [internal]), no reason

My connection is established as:

uri = 'wss://api.dydx.exchange/v3/ws'
connection = websockets.connect(uri=uri, ping_interval=None, ping_timeout=None)
...
async with connection as websocket:
   ...
   async for message in websocket:
      ...
      handling the message here

From what I can see, on your documentation here (https://github.com/dydxprotocol/dydx-v3-python/blob/master/examples/websockets.py) there is no handling of ping messages from the server.

Addtionally, from cursory review of similar issues online, my problem seems to happen on lack of pings from the client, but I'm sending those and receiving PONGs back.

I am also not receiving any ping messages from the server as https://dydxprotocol.github.io/v3-teacher/?json#v3-websocket-api mentions.

I have tried to send unsolicited pong messages to the server, but that doesn't work either, as they are not accepted on the channel receiving the following response: {'type': 'error', 'message': 'Invalid message type: pong', 'connection_id': 'xxxx-xxxx-xxx', 'message_id': 2}

Finally, after some time the connection becomes stale in one of two states:

  1. I send pings and don't receive pongs and no updates are send to the client from the websocket server
  2. I send pings, I receive pongs but no updates are send to the client from the websocket server

I handle these cases and reconnect. However, I'm going for as much uptime as possible, so I'm trying to understand why my client gets disconnected. Especially since I have tested and you can have a lot of subscriptions (>50) to the same channel from the same IP (as long as your respect the rate limiting requirements).

I'm using Python 3.9.

Do you, maybe, have any help?

Lqz13Th commented 1 year ago

you should sent ping instead pong

Lqz13Th commented 1 year ago

but i send ping and get pong from dydx. still have1006 error, sometimes it shows another error called 'v3_accountsTimestamp must be within 30 seconds of the server time' I am not sure which part cause this problem