M
Hello @dlmandalia,
Here is an example of python code to be used for using Websocket Streaming 2.0:
AUTH_TOKEN = "token"
API_KEY = "key"
CLIENT_CODE = "code"
FEED_TOKEN = "feed_token"
correlation_id = "abc123"
action = 1
mode = 3
token_list = [{"exchangeType": "1", "tokens": ["438"]}, {"exchangeType": "1", "tokens": ["13528"]}, {"exchangeType": "1", "tokens": ["18365"]}, {"exchangeType": "1", "tokens": ["5701"]}, {"exchangeType": "1", "tokens": ["2142"]}, {"exchangeType": "1", "tokens": ["881"]}, {"exchangeType": "1", "tokens": ["29135"]}, {"exchangeType": "1", "tokens": ["21770"]}, {"exchangeType": "1", "tokens": ["11957"]}, {"exchangeType": "1", "tokens": ["3351"]}, {"exchangeType": "1", "tokens": ["11532"]}, {"exchangeType": "1", "tokens": ["1964"]}, {"exchangeType": "1", "tokens": ["3432"]}, {"exchangeType": "1", "tokens": ["21808"]}, {"exchangeType": "1", "tokens": ["404"]}, {"exchangeType": "1", "tokens": ["20374"]}, {"exchangeType": "1", "tokens": ["10738"]}, {"exchangeType": "1", "tokens": ["24948"]}, {"exchangeType": "1", "tokens": ["4306"]}, {"exchangeType": "1", "tokens": ["685"]}, {"exchangeType": "1", "tokens": ["1174"]}, {"exchangeType": "1", "tokens": ["2412"]}, {"exchangeType": "1", "tokens": ["10447"]}, {"exchangeType": "1", "tokens": ["14299"]}, {"exchangeType": "1", "tokens": ["17094"]},]
#retry_strategy=0 for simple retry mechanism
sws = SmartWebSocketV2(AUTH_TOKEN, API_KEY, CLIENT_CODE, FEED_TOKEN)
def on_data(wsapp, message):
logger.info("Ticks: {}".format(message))
# close_connection()
def on_control_message(wsapp, message):
logger.info(f"Control Message: {message}")
def on_open(wsapp):
logger.info("on open")
some_error_condition = False
if some_error_condition:
error_message = "Simulated error"
if hasattr(wsapp, 'on_error'):
wsapp.on_error("Custom Error Type", error_message)
else:
sws.subscribe(correlation_id, mode, token_list)
# sws.unsubscribe(correlation_id, mode, token_list1)
def on_error(wsapp, error):
logger.error(error)
def on_close(wsapp):
logger.info("Close")
def close_connection():
sws.close_connection()
# Assign the callbacks.
sws.on_open = on_open
sws.on_data = on_data
sws.on_error = on_error
sws.on_close = on_close
sws.on_control_message = on_control_message
sws.connect()
Hope this helps to resolve your query.
Regards
SmartAPI Team