• Python 3.8
  • I'm using mingw on msys2 on Win10
I want to solve.

I would like to create a class that fetches transaction information sequentially using CoinCHeck's API.
But when I run it in the code below, nothing is displayed. ..

import json
from websocket import WebSocketApp

class CoinCheck_CandleMaker:
    URL = "wss: //ws-api.coincheck.com/"
    MAX_CANDLE_LEN = 24 * 60
    def __init __ (self, t_sec = 60):
        self.__span_T = t_sec
        self._candle = []
        self.__connect ()
    def __connect (self):
        self.ws = WebSocketApp (self.URL,
                                on_message = self.__ on_message,
                                on_open = self.__ on_open,
                                on_close = self.__ on_close,
                                on_error = self.__ on_error
        try: try:
            self.ws.run_forever ()
        except KeyboardInterrupt:
            self.ws.close ()
    def __on_open (self, ws):
        print ("open")
        self.ws.send (
            json.dumps ({{
                "type": "subscribe",
                "channel": "btc_jpy-trades"
    def __on_message (self, ws, message):
        print (message)
    def __on_close (self, ws):
        print ("close")
    def __on_error (self, ws, error):
        print (error)
        self.__ reconnect ()
    def __reconnect (self):
        print ("Try reconnect")

if __name__=="__main__":
    test = CoinCheck_CandleMaker ()

When I run the code below, it does what I expect.

import json
from websocket import WebSocketApp
def On_open (ws):
        print ("open")
        ws.send (
            json.dumps ({{
                "type": "subscribe",
                "channel": "btc_jpy-trades"
def On_message (ws, message):
    print (message)

ws = WebSocketApp ("wss: //ws-api.coincheck.com/", on_message = On_message, on_open = On_open)
ws.run_forever ()
[164127931, "btc_jpy", "3287382.0", "0.01", "buy"]
[164127932, "btc_jpy", "3287382.0", "0.005", "buy"]
[164127933, "btc_jpy", "3287397.0", "0.072", "buy"]
[164127934, "btc_jpy", "3287397.0", "0.2", "buy"]
[164127935, "btc_jpy", "3286636.0", "0.0158", "sell]
What's wrong? ??

I would appreciate it if you could point it out, but it doesn't matter if you try

  • Answer # 1

    Probably the fundamental answer

    Your websocket-client hasn't been maintained for a long time, so use the maintained websockets.

    Probably not the fundamental answer

    For exampleon_openIf you pass a function to, this library behaves differently depending on whether it is a "just a function" or an "object method". In the case of a simple function, in the first argument of the passed functionWebSocketAppofselfPass.. It was written in the documentdef on_open (ws):That was what it was. If it was an object methodselfJust don't pass. This operation is written around here.

    Of the code that hidekiti gavedef __on_open (self, ws):Although it is a declarationwsWas superfluous. It wasn't necessary because we were passing the method of the object.