I have an app which uses both REST API and WebSocket API for years. Since Feb 12 I got connection closed on connected WebSocket which I don’t use for more than 2 minutes. I know there is an information saying that WebSocket will be sending ping packet and will require pong, but I am not getting any single message during more than 2 minutes, so I don’t have any ping to reply to.
2025-01-28
Notice: These changes will be gradually rolled out between February 3, 2025 and February 14, 2025.The following changes will apply to WebSocket Market Data Streams, User Data Streams, and the WebSocket API:
Our WebSocket services will send a ping frame every 20 seconds instead of 3 minutes.
The allowed pong delay will be every 1 minute instead of 10 minutes.
The documentation for these services have been updated to reflect the change.
This is the call/request flow:
REST API /v3/account?timestamp=…&signature=…
REST API /v3/userDataStream → getting listenkey
WS API connect wss://stream.binance.com:9443/stream?streams=
REST API /v3/ticker/24hr
Doing more than 2 minutes of: REST API /v3/klines?symbol=&interval=15m&limit=99
During that I got WebSocket connection closed.
If I do not do any of steps 4., 5., I will not get connection closed and WS will be active until I disconnect.
There is nothing changed (as far as I know) on my side. Could you review the steps above and advice what could be the reason for that behavior, please? Should I be doing something in a different way, please?
Given Binance’s recent changes to WebSocket behavior, here are a few possible explanations and recommendations:
Possible Causes:
New WebSocket Timeout Rules
Binance now sends a ping every 20 seconds instead of 3 minutes. If a pong response is not received within 1 minute (instead of the previous 10 minutes), the connection may be closed.
If your WebSocket is not receiving pings or failing to reply, it could trigger a disconnect.
Increased Load When Calling v3/klines
The v3/klines endpoint can be resource-intensive, especially when requesting a large time frame. If this call takes too long to return, it could delay WebSocket activity, causing an unintended disconnect.
Rate Limiting or Connection Conflicts
Making frequent REST API requests alongside WebSocket connections might be hitting some limit or causing temporary network congestion, leading to WebSocket closure.
Test whether lowering the frequency of v3/klines calls prevents WebSocket disconnections.
Implement Auto-Reconnection for WebSocket:
If the WebSocket disconnects, ensure your app automatically reconnects.
Network Troubleshooting:
Try running the same setup on a different network to rule out any ISP-level blocking or interference.
If possible, could you log and share the WebSocket messages received before the disconnect? This could provide more insights into whether Binance is closing the connection due to an unacknowledged ping or another reason.