python-kucoin: ReconnectingWebsocket is not reconnecting

Hi there, I have the issue that the websocket seems to be closed after approx 60 seconds. I subscribed to a 3 tickers and my callback function informs me when it was not called for at least 15 sec. The first minute everything works fine, but then I do not receive messages anymore from the websocket. When I then run ksm.unsubscribe for my tickers, I get:

  File "C:/Users/xxxxxxxxxx", line 307, in kunsubscribe
    await self.ksm.unsubscribe('/market/ticker:ETH-BTC')

  File "C:\ProgramData\Anaconda3\lib\site-packages\kucoin\asyncio\websockets.py", line 239, in unsubscribe
    await self._conn.send_message(req_msg)

  File "C:\ProgramData\Anaconda3\lib\site-packages\kucoin\asyncio\websockets.py", line 133, in send_message
    await self._socket.send(json.dumps(msg))

  File "C:\ProgramData\Anaconda3\lib\site-packages\websockets\protocol.py", line 361, in send
    yield from self.ensure_open()

  File "C:\ProgramData\Anaconda3\lib\site-packages\websockets\protocol.py", line 501, in ensure_open
    self.close_code, self.close_reason) from self.transfer_data_exc

ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason

This error does not appear when I unsubscribe from the websocket before the 1 minute has passed. So altogether it seems the ReconnectingWebsocket class you created does not reconnect in this case as the Websocket connection is down according to the error. Any idea what is wrong?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@MarcelBeining My code fails randomly sometimes after 6 hours and sometimes after 2 days. i have about 99 subscriptions.

For now I use the following code in case it lost the connection:

async def main():
    global loop, nr_no_events, fifo_buffer
    
    # callback function that receives messages from the socket
    async def handle_evt(msg):
        global nr_no_events
        global fifo_buffer
        ....
        ....
        nr_no_events = 0
        
    client = Client(api_key, api_secret, api_passphrase, sandbox=False)
    ksm = await KucoinSocketManager.create(loop, client, handle_evt)
    
    await ksm.subscribe('/market/match:'+sys.argv[1])
    
    while True:
        print(nr_no_events, "sleeping to keep loop open")
        nr_no_events = nr_no_events+1
        if(nr_no_events>2):
            try:
                try:
                    await ksm.unsubscribe('/market/match:'+sys.argv[1])
                except:
                    pass #possibly nothing to unsubscribe
                #resubscribe
                client = Client(api_key, api_secret, api_passphrase, sandbox=False)
                ksm = await KucoinSocketManager.create(loop, client, handle_evt)
                await ksm.subscribe('/market/match:'+sys.argv[1])
                nr_no_events = 0
            except:
                pass
        await asyncio.sleep(20)

Small note: I am not the owner of this repository.

@Full4me jep you can. I can subscribe to 100 markets per instance at the moment with the same API and ip