Skip to content

WebSocket subscribe() sends all streams in every loop iteration, breaking 5+ stream subscriptions #478

@natanr123

Description

@natanr123

Description:

Description

When subscribing to multiple kline streams using binance-sdk-spot v6.0.0, messages stop being received when subscribing to 5 or more streams. The issue is in the subscribe() method in binance_common/websocket.py.

Bug Location

In binance_common/websocket.py, the subscribe() method (around lines 512-534) has a loop that sends ALL streams in every iteration instead of just the current stream:

for stream in streams:                                                                                                                                                                                                                                                                                   
    # ...                                                                                                                                                                                                                                                                                                
    json_msg = {"method": "SUBSCRIBE", "params": streams, "id": request_id}  # BUG: sends all streams                                                                                                                                                                                                    
    await self.send_message(json_msg, connection)                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                         
This should be "params": [stream] (single stream) instead of "params": streams (all streams).                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                         
Steps to Reproduce                                                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                         
from binance_sdk_spot.spot import Spot                                                                                                                                                                                                                                                                   
import asyncio                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                         
async def test():                                                                                                                                                                                                                                                                                        
    client = Spot()                                                                                                                                                                                                                                                                                      
    connection = await client.websocket_streams.create_connection()                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                         
    symbols = ['btcusdt', 'ethusdt', 'xrpusdt', 'ltcusdt', 'uniusdt', 'bnbusdt']  # 6 symbols                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                         
    for symbol in symbols:                                                                                                                                                                                                                                                                               
        stream = await connection.kline(symbol=symbol, interval="1m")                                                                                                                                                                                                                                    
        stream.on("message", lambda data: print(f"Received: {data}"))                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                         
    await asyncio.sleep(60)  # No messages received                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                         
asyncio.run(test())                                                                                                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                                         
Expected Behavior                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                         
Messages should be received for all 6 symbols.                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                         
Actual Behavior                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                         
- 1-4 symbols: Works correctly                                                                                                                                                                                                                                                                           
- 5+ symbols: No messages are received                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                         
Workaround                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                         
Using the combined stream URL directly works correctly:                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                         
import aiohttp                                                                                                                                                                                                                                                                                           
url = "wss://stream.binance.com:9443/stream?streams=btcusdt@kline_1m/ethusdt@kline_1m/..."                                                                                                                                                                                                               
# This works for all symbols                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                         
Environment                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                         
- Package: binance-sdk-spot v6.0.0                                                                                                                                                                                                                                                                       
- Python: 3.13                                                                                                                                                                                                                                                                                           
- OS: Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions