Websocket Response

Hello,

I used the following code to get the Symbol Update

https://github.com/FyersDev/fyers-api-sample-code/blob/sample_v3/v3/python/websocket/data_socket/data_websocket_background.py

But I am receiving such response instead of symbol data. Could anyone please help here?

Response: {‘type’: ‘ful’, ‘code’: 200, ‘message’: ‘Full Mode On’, ‘s’: ‘ok’}

Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}

First to test, use the foreground code so that you get print in stdout instead of file

next, test when market is open for the symbol you are testing

3rd, add channel argument in this line

fyers.subscribe(symbols=symbols, data_type=self.data_type, channel=15)

Hello Ji,

I am printing on stdout only not to any file. What do you mean by foreground code?

Thanks.

Hello Ji,

I have printing to stdout only not to any file. May I know what do you mean by foreground code?

Thanks.

@Z4PBvkQGyF ji, pls help here

the code that you taken is data_websocket_background.py

in same github dir, there is sample-code/v3/python/websocket/data_socket/data_websocket_foreground.py at sample_v3 · FyersDev/fyers-api-sample-code (github.com)

bcos i dont know your python skill level

anyway, see the other points i wrote

read my post and add channel parameter

I’ve use the both the code and still getting same response during live market also. In fact, I tried with lite mode too…here is the reponse from lite mode …

Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: ‘ok’}

Response: {‘type’: ‘lit’, ‘code’: 200, ‘message’: ‘Lite Mode On’, ‘s’: ‘ok’}

Response: {‘type’: ‘cn’, ‘code’: 200, ‘message’: ‘Authentication done’, ‘s’: 'o

There is no data returning just a response code of 200.

@Z4PBvkQGyF ji pls help

Regarding python skill, I have been a coder since 2 years

@kunal.9h6jpjsw
Please try with extra channel parameter while the subscribing

fyers.subscribe(symbols=symbols, data_type=self.data_type, channel=15)

@Z4PBvkQGyF ji…tried that as well…attached is my code…pls replace it with your access code and try..here it my code..

https://github.com/bagri-apsk/market-data

because your order is wrong

fyers.subscribe(symbols=symbols, data_type=data_type, channel=15)

    # Keep the socket running to receive real-time data
  #  fyers.keep_running()
fyers.connect()

I hope you understand the order in which function is called matters.

First connect then call subscribe

you are calling connect afterwards which is wrong, first fyers.connect() then fyers.subscribe()

@kunal.9h6jpjsw
This try
https://github.com/FyersDev/fyers-api-sample-code/blob/sample_v3/v3/python/websocket/data_socket/data_websocket_foreground.py

You are calling wrong.

@Z4PBvkQGyF

Can you pls tell me how this code will work? “fyers” object is used before its creation

symbols = [‘NSE:SBIN-EQ’, ‘NSE:ADANIENT-EQ’]

** fyers.subscribe(symbols=symbols, data_type=data_type) —> You are using fyers object here**

# Keep the socket running to receive real-time data

fyers.keep_running()

Replace the sample access token with your actual access token obtained from Fyers

access_token = “XCXXXXXXM-100:eyJ0tHfZNSBoLo”

Create a FyersDataSocket instance with the provided parameters

**fyers = data_ws.FyersDataSocket( ----> but creating **

access_token=access_token,       # Access token in the format "appid:accesstoken"

log_path="",                     # Path to save logs. Leave empty to auto-create logs in the current directory.

litemode=False,                  # Lite mode disabled. Set to True if you want a lite response.

write_to_file=False,              # Save response in a log file instead of printing it.

reconnect=True,                  # Enable auto-reconnection to WebSocket on disconnection.

on_connect=onopen,               # Callback function to subscribe to data upon connection.

@kunal.9h6jpjsw
Please follow above code.

@Z4PBvkQGyF

this is what I got …

** File “C:\Users\User\PycharmProjects\pythonProject2\main.py”, line 42, in onopen**

** fyers.subscribe(symbols=symbols, data_type=data_type, channel=15)**

UnboundLocalError: local variable ‘fyers’ referenced before assignment

you are not reading properly what has been told.

Forget your code, read my 1st post, take the one from github and start from there. Do proper code review, this much code proficiency is expected.

Websocket code runs in a different thread, all those functions are callbacks and they will be triggered by events in the Fyers module that you cant see.

You have to init class, then connect and lastly subscribe.

@chirag @Z4PBvkQGyF this is my code changes after your suggestions but still same output. Please check

https://github.com/bagri-apsk/market-data

If possible do the necessary changes and checkins those.