Unsubscribe Instruments

DM2290
Hi @sujith,

Final hurdle to my application - I am trying to unsubscribe instruments based on user input and have written the following code on user button click -
 ticker.unsubscribe(parseInt(requiredTokenToBeRemoved)); // where requiredTokenToBeRemoved is the value to be removed from the items array 
ticker.setMode(ticker.modeLTP, items); // where items has been defined initially and returning ticks continuously

ticker.disconnect();
ticker.connect();
ticker.on("connect", subscribe);
ticker.on("ticks", onTicks);
but the above code does not work and my initial list of subscribed tokens ticks are only getting returned. Please advice.
Tagged:
  • tonystark
    If you just want to unsubscribe, just `ticker.unsubscribe` call is enough. Also make sure to send an array to this. Docs here.

    Rest of the code is not necessary.
  • DM2290
    Thanks @tonystark but the following code does not work either -
      var unsubscribeTokens = [15020290];
    ticker.unsubscribe(unsubscribeTokens);
    Should the unsubscribe be called in the same function where subscribe was called? I have declared ticker at a global level and tried to unsubscribe in a different function call i.e based on user input for a particular token.

    @tonystark , @sujith , @rakeshr - can you please share any example code where unsubscribe is being used in a different function call rather than in the same function.

    Thanks so much! This is the final step in my application and the only issue pending.
  • rakeshr
    Should the unsubscribe be called in the same function where subscribe was called? I have declared ticker at a global level and tried to unsubscribe in a different function call i.e based on user input for a particular token.
    Go through this thread.
  • DM2290
    Thanks @rakeshr,

    From the examples given there, I can infer that unsubscribe is called within on_ticks but won't that check happen every time a tick is received? I want to unsubscribe only after user input and don't need that check every time ticks are received. Can you please suggest how to do that?
  • DM2290
    Hi @rakeshr, @sujith, @tonystark ,

    Any help on this please?
  • tonystark
    tonystark edited November 2023
    Could you post a trimmed down version of your code that can reproduce this issue?

    We can't provide much help without knowing what exactly is the logic. From your description "I want to unsubscribe only after user input" this seems to be very straightforward, simply calling "ticker.unsubscribe" after user input/submit should do what you are looking for. There are no other restrictions like call from same function or on_tick to unsubscribe. You can call that from anywhere.
  • DM2290
    Thanks @tonystark - for some reason the unsubscribe didn't work earlier. It works now. This thread can be closed now.
This discussion has been closed.