custom message function is not getting called

custom message function is not getting called to receive the Symbol update. The response is showing on console. I even tried with run_background flag as “True”, then also** **“custom_message” function not getting called

fyers-apiv2, Version: 2.0.5

@P5riXHjRDF

data_type = "symbolData"
symbol= ["NSE:SBIN-EQ"]

def custom_message(msg):
     print ("Custome Message " + str(msg.response)) 

ws.websocket_data = custom_message
# ws.FyersSocket.websocket_data = custom_message
fyersSocket = ws.FyersSocket(access_token=access_token,run_background=False,log_path="D:/")
fyersSocket.subscribe(symbol=symbol,data_type=data_type)

HI Ameet Sir,

Sorry for the delay reponse,

Please follow the below git link for the required python code to connect to websocket.

https://github.com/FyersDev/fyers-api-sample-code/blob/sample_v2/My%20API%20Sample%20Script(GET%20STARTED)/websocket%20sample%20script/websocket_foreground.py

It works,

Hope this helps,

Happy Trading.

Go to ws.py in Fyers library and call the callback in __ on _ message(). Somehow they missed it when in background mode and only print data to logger.

if self.websocket_data is not None:						
  self.websocket_data(self.response)
else:
  self.logger.debug(f"Response:{self.response}")

Hi Sir,
Thanks for your valuable feedback,
We are working on it and will be providing the fixes in next built.

Thanks for reply,

On first comment you replied as “it works” and now you saying you are providing fixes.

As expected very poor support from fyers. My few queries are still answered on my email, Fyers only create tickets for any issue and do not solve it.

Hi Sir,

As mentioned in the first,
If One follow the code structure provided below

https://github.com/FyersDev/fyers-api-sample-code/blob/sample_v2/My%20API%20Sample%20Script(GET%20STARTED)/websocket%20sample%20script/websocket_foreground.py

It will surely work and will give proper response for the new built. We provided solution as client was facing the issue.

Here in the code provided in the query ““print (“Custome Message " + str(msg.response))””
in this line , we made changes and uploaded the working sample code in git, where one can get the custom msg printed by removing the response attribute like this, ““print (f"Custom:{msg}”)”” .

SO its our responsibility to take feedback from client, so that we can make our product better in upcoming built.

Hope this helps Sir,
Happy Trading

how do we customise the msg in (f"Custom:{msg}")?
i have tried multiple approach but still fails.
can u help by providing a sample of a way to custom it?

Hi Ameet,

Seems like Fyers has got it wrong here, when they say.

  1. run_background=True mode, they will only print the messages

  2. run_background=False, you will not get back control of the main thread as WS runs in main thread.

probably they do not know how production trading works. They also have made distinction between runbackground and background_flag class attributes, which is bit difficult to understand why.

I think best option for now is

  • after subscribe make fyersSocket.background_flag = False

symbol = ['MCX:CRUDEOIL22FEBFUT', 'MCX:GOLD22APRFUT']

fyersSocket.subscribe(symbol=symbol, data_type=data_type)

fyersSocket.background_flag = False

Caveat : But this might not not work good with the multiple calls to subscribe will make background_flag=True for some time and you will miss few updates in between. so second clean solution with extra code.

  1. to override class FyersSocket with its private method __ on * message() which raises the websocketdata callback irrespective of backgoundflag. please see how I have inherited private method with FyersSocket prefix for any priavate attribute with ‘’*__" in front of in FyersSocket

class CustomFyersSocket(FyersSocket):

def __init__(self, *args, **kwargs):

FyersSocket.__init__(self, *args, **kwargs)

def _FyersSocket__on_message(self, ws, msg):

if self._FyersSocket__data_type == "symbolData":

self.response = self.parse_symbol_data(msg)

if type(msg) == str:

if "error" in msg:

msg = json.loads(msg)

self.response_out["s"] = msg["s"]

self.response_out["code"] = msg["code"]

self.response_out["message"] = msg["message"]

self.response = self.response_out

self.logger.error(self.response)

if self.websocket_data is not None:

self.websocket_data(self.response)

else:

print(f"Response:{self.response}")

else:

if self.websocket_data is not None:

self.websocket_data(self.response)

else:

print(f"Response:{self.response}")

# self.logger.debug(self.response)

else:

self.response = msg

if type(msg) == str:

if "error" in msg:

msg = json.loads(msg)

self.response_out["s"] = msg["s"]

self.response_out["code"] = msg["code"]

self.response_out["message"] = msg["message"]

self.response = self.response_out

self.logger.error(self.response)

if self.websocket_data is not None:

self.websocket_data(self.response)

else:

print(f"Response:{self.response}")

else:

if self.websocket_data is not None:

self.websocket_data(self.response)

else:

print(f"Response:{self.response}")

else:

if self.websocket_data is not None:

self.websocket_data(self.response)

else:

print(f"Response:{self.response}")

# self.websocket_data()

return

# if self.__ws_message is not None:
# self.__ws_message(msg)