Implementation Streaming Data Function


  • Dear,
    As per your video streaming in Implementation Streaming Data Function, it is not working in my Spyder, it gets error and stop the process. Kindly guide over this, so i go further practicing to your videos.


  • Hello @dipesh-khatri,

    Please try using this code for websocket streaming 2.0

    Let us know if you have any issues.

    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": ["13285"]}]
    
     
    #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()
    

    Thank you

    Regards
    SmartAPI Team