0

Having the following design problem: I open a websocket connection and send multiple messages every second to get open risk.

What’s happening is the message queue is building up wit a lot of “stale” messages (from frequently sending messages), and when I send a trade order out and it fills, the risk is delayed as it has to wait for the while loop to iterate through all the old messages, before it updates with the correct risk

Ie, the deque in websockets["messages"] is getting really long, and it takes the while loop some time to catch up to the "recent" messages

Sample code:

import websockets
url = 'wss://www.deribit.com/ws/api/v2'

risk1 = None
risk2 = None

async with websockets.connect(url) as ws:

     # send many messages every second, which builds up a lot of messages in the queue
     await send_message("private/get_positions", "btc-perpetual")
     await send_message("private/get_positions", "eth-perpetual")
     ....
     await send_message("private/get_positions", "sol-perpetual")

     # messages from above build up in a deque, which gets iterated one-at-a-time in while loop
     response = await ws.recv()
     if response["id"] == 100:
         risk1 = response["result"]
     elif response["id"] == 200:
         risk2 = response["result"]
     else:
         pass

     # message queue gets long, and messages go stale (response from ws.recv() ), resulting in out-of-date risk
     risk_usd = calculate_risk(risk1, risk2)
     if risk_usd > 0:
          await post_order()

some ideas ive had, but not sure if good practice:

  • ignore message if > x seconds
  • unpack the websockets["messages"] and choose last item

note: there are multiple variables (risk1, risk2) getting updated with each iteration, and ALL of them need to be up-to-date

1
  • Three possible ideas. One is that the receiver might do batch processing and you could "batch" a group of messages. Another, count open messages and don't send at a high-water mark. Yet another, two connections, one for reading - can be slow, one for trades - want to be fast.
    – jwal
    Oct 24, 2022 at 9:20

1 Answer 1

0

Maybe try to do

await asyncio.sleep

instead of doing

asyncio.sleep

on the server side, which simply don't waits before sending if you forget the await keyword.

I hope having understood what's your problem, but maybe I am totally wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.