Search code examples
pythonwebsocketbinancebinance-api-client

Binance Websocket client stops after a while


I am working on Binance Websocket to listen to account events MARKET events. I copy every order from masters to salve it works just fine when I run it and it copies from all master. However, when I run it in background using nohup Linux service it stops reading any events after 3 days of running the script. I tried to capture the STDERR but nothing is piped from the script. Even though it is appeared in htop as a running process but without doing its job. I tried to use asyncio framework but I got the same problem. I need to listen to multi-master at the same time and in a long-run term.

import csv
import math
import threading
import time
from datetime import datetime

from binance import ThreadedWebsocketManager, Client

masters = [
    [
        'api1',
        'secret1',
        'test1'
    ],
    [
        'api2',
        'secret2',
        'test2'
    ],
]

def CopyOrder(order, client, name):
    # some code to copy order from master
def main(api_key: str, api_secret: str, name: str):
    print("Start listening on", name) # Debug

    twm = ThreadedWebsocketManager(api_key=api_key, api_secret=api_secret) 
    cl = Client(api_key=api_key, api_secret=api_secret, testnet=False) 
    twm.start() # Start the websocket manager

    def handle_socket_message(msg):
        if msg['e'] == 'executionReport':
            # check if it is order or not
            CopyOrder(order=msg, client=cl, name=name)
            


    twm.start_user_socket(callback=handle_socket_message)
    twm.join()


if __name__ == "__main__":

    for master in masters:
        # listen to all masters for new orders
        thread = threading.Thread(target=main, args=(master[0], master[1], master[2]))
        thread.start()

Solution

  • Quoting from Binance https://binance-docs.github.io/apidocs/spot/en/#websocket-market-streams

    A single connection to stream.binance.com is only valid for 24 hours; expect to be disconnected at the 24 hour mark

    I am working with Binance Websocket too and I noticed the same random disconnection, sometimes after a day, sometimes after a few days, so I now have an uptime check in place and I manage a disconnection/reconnection