How BinanceSocketManager works
masterThe BinanceSocketManager handles all websocket connections. You initialize it by passing an existing API client.
Key behaviors:
- Multiple Connections: You can open multiple socket connections through one manager.
- Singleton Socket Types: Only one instance of a specific socket type per symbol is allowed (e.g., you cannot have two
BNBBTCDepth sockets, but you can have oneBNBBTCDepth and oneBNBBTCTrade socket simultaneously). - Callbacks: Every socket requires a callback function that is executed whenever a message is received. Messages are provided as dictionary objects.
- Automatic Reconnection: Websockets are configured to attempt reconnection with a maximum of 5 retries.
- Lifecycle: You must start individual sockets before calling
bm.start()to begin the manager's execution loop.
from binance.websockets import BinanceSocketManager
# Initialize manager with client
bm = BinanceSocketManager(client)
# 1. Define callback
def process_message(msg):
print(msg)
# 2. Start specific sockets
conn_key = bm.start_trade_socket('BNBBTC', process_message)
# 3. Start the manager
bm.start()