Binance Websocket client stops after a while

Question:

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()
Asked By: Hassan Ibraheem

||

Answers:

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

Answered By: Spino

I did the following, stop it and start it again every 1 hour.

while true:

 user_socket = twm.start_user_socket(callback=handle_socket_message)

 time.sleep(3600)

 self.twm.stop_socket(user_socket)

but I found another problem, I will open threads indefinitely, in a week I have more than 100, it increases the memory and the cpu, I do not know why they do not close.
If someone has the answer, I appreciate it.

1 MainThread 2760 False 11

2 ThreadPoolExecutor-0_0 20836 True 11

3 ThreadPoolExecutor-1_0 24580 True 11

4 ThreadPoolExecutor-0_1 18608 True 11

5 ThreadPoolExecutor-1_1 26140 True 11

6 ThreadPoolExecutor-1_2 12320 True 11

7 ThreadPoolExecutor-1_3 16352 True 11

8 ThreadPoolExecutor-1_4 26160 True 11

9 ThreadPoolExecutor-1_5 23196 True 11

10 ThreadPoolExecutor-1_6 15032 True 11

11 ThreadPoolExecutor-1_7 25932 True 11

the problem is that they don’t close and I don’t know how to do it.

There are 2 things worths to check:

  1. User’s listenKey should be renewed every 60 minutes.
  2. Client should send PONG when receiving PING message from server.
Answered By: Binance