I'm trying to create a script using asyncio
and websocket
that should connect to 4-5 cryptocurrency exchange websockets and receive trades in real time from those exchanges. My code works and it's very simple, it looks like this:
import asyncio
import websockets
import json
subscriptions = ['btcusdt@trade', 'ethusdt@trade', 'bchusdt@trade', 'xrpusdt@trade', 'eosusdt@trade', 'ltcusdt@trade', 'trxusdt@trade', 'etcusdt@trade', 'linkusdt@trade', 'xlmusdt@trade', 'adausdt@trade', 'xmrusdt@trade', 'dashusdt@trade', 'zecusdt@trade', 'xtzusdt@trade', 'bnbusdt@trade', 'atomusdt@trade', 'ontusdt@trade', 'iotausdt@trade', 'batusdt@trade', 'vetusdt@trade', 'neousdt@trade', 'qtumusdt@trade', 'iostusdt@trade', 'thetausdt@trade', 'algousdt@trade', 'zilusdt@trade', 'kncusdt@trade', 'zrxusdt@trade', 'compusdt@trade', 'omgusdt@trade', 'dogeusdt@trade', 'sxpusdt@trade', 'kavausdt@trade', 'bandusdt@trade', 'rlcusdt@trade', 'wavesusdt@trade', 'mkrusdt@trade', 'snxusdt@trade', 'dotusdt@trade', 'defiusdt@trade', 'yfiusdt@trade', 'balusdt@trade', 'crvusdt@trade', 'trbusdt@trade', 'yfiiusdt@trade', 'runeusdt@trade', 'sushiusdt@trade', 'srmusdt@trade', 'bzrxusdt@trade', 'egldusdt@trade', 'solusdt@trade', 'icxusdt@trade', 'storjusdt@trade', 'blzusdt@trade', 'uniusdt@trade', 'avaxusdt@trade', 'ftmusdt@trade', 'hntusdt@trade', 'enjusdt@trade', 'flmusdt@trade', 'tomousdt@trade', 'renusdt@trade', 'ksmusdt@trade', 'nearusdt@trade', 'aaveusdt@trade', 'filusdt@trade', 'rsrusdt@trade', 'lrcusdt@trade', 'maticusdt@trade', 'oceanusdt@trade', 'cvcusdt@trade', 'belusdt@trade', 'ctkusdt@trade', 'axsusdt@trade', 'alphausdt@trade', 'zenusdt@trade', 'sklusdt@trade']
async def connect():
while True:
async with websockets.client.connect('wss://fstream.binance.com/ws/trade') as ws:
tradeStr = {"method": "SUBSCRIBE", "params": subscriptions, 'id': 1}
await ws.send(json.dumps(tradeStr))
while True:
try:
msg = await asyncio.wait_for(ws.recv(), 5)
message = json.loads(msg)
try:
print(message)
except Exception as e:
print(e)
except asyncio.TimeoutError:
break
asyncio.get_event_loop().run_until_complete(connect())
In the example above, i'm connecting to Binance and i'm receiving trades for all the markets available. I do this for more exchanges at once, but the problem will happen with one too as long as i'm receiving a lot of messages per second.
Each message looks like this {"rate": "xx", "market": "xx", "amount": "xx", "side": "xx"}
, so very small.
The big problem i'm noticing is that after a while the script is running, i start receiving less messages, a lot of them will come after a lot of seconds and i don't even receive a lot others, as if they get lost or as if the connection is freezing.
Now, i know that it's not a very specific question, but what could be the problem here?
Is it possible that when websockets receive a lot of messages per second there could be problems of this kind? I tried to test this system from my local and from a vps, and in both cases i encountered the same issues. Is it possible that this is a resource problem? Or is it most likely related to the server, and not the client which is me? I tried to be as specific as possible, i can be more detailed if needed. I read that websockets
stores received messages in a buffer. Is it possible that the problem is with the buffer getting filled? Any kind of advice is appreciated!
from Can websockets "lag" when a lot of messages are received?
No comments:
Post a Comment