Hi. I have a few questions about Websocket Market Streams for Futures:
I get disconnected pretty often some days more often then others can be every 5-8 minutes. I don’t seem to receive the ping frames so I don’t send pongs back. Can this be an issue for the disconnects?
Is it ok to connect to many Futures Websocket Market Streams from a single machine? Can this be an issue for the disconnects?
Thanks for the reply. I wrote the javascript client using native browser WebSocket API.
I am connecting to ‘wss://fstream.binance.com/ws/’. On top of it stream '‘wss://fstream.binance.com/ws/{symbol}@depth’ also disconnects many times sporadically. I am having disconnects way to often. What can I do to improve this aspect? If I lose User Data Stream I am missing the important order updates. There’s gotta be a solution to this. Please advise.
This is the implementation I have. I decided to use a single WebSocket connection for @depth and account updates. Some days are worse than others. I connected over the wire now rather than wife and still get disconnected sporadically. Here is the code. Please let me know where it can be improved.
let refreshInterval;
const listenKey = // getting a listenKey here using POST /fapi/v1/listenKey
const ws = new WebSocket(“wss://fstream.binance.com/ws/” + listenKey);
ws.onclose = async () => {
clearInterval(refreshInterval);
// handling reconnect here. it happens many very randomly sometimes multiple times an hour - sometimes not as often - the least is probably a few times a day.
};
When I use ClientWebSocket in C# I still get disconnected even though it handles the PING/PONG for me.
One thing that I figured out is that I need to space my subscriptions out a little bit (200ms) in order for the connection to not get reset. The docs mention that you can only send 5 messages per second:
WebSocket connections have a limit of 5 incoming messages per second. A message is considered:
I suspect this was causing my issues because I was subscribing in a loop without a delay between each sub.
The reason I connected them in a single ws connection was b/c both were closing frequently and I needed reconnect so it was easier to deal with a single connection. Thanks for the advice!
So you recommend to connect to USER_DATA_STREAM and MARKET_STREAM separately? What kind of ping frames you recommend to send for USER_DATA_STREAM - something like this is ok ws.send(‘ping’) ?