TradingView-API icon indicating copy to clipboard operation
TradingView-API copied to clipboard

How do I reconnect to client after my internet reconnects?

Open alikabeer32 opened this issue 3 years ago • 3 comments

Right now the api stops updating even after the internet is back


client.onDisconnected(() => {

  console.log("client DISconnected!");

  setTimeout(() => {

    // How to reconnect here?
    console.log('connected?!');

  }, 5000);
});

alikabeer32 avatar Jul 24 '22 15:07 alikabeer32

@Mathieu2301 can you please help me out here?

alikabeer32 avatar Jul 24 '22 17:07 alikabeer32

@Mathieu2301 can you please help me out here?

client.isOpen returns false when I check after internet is back

alikabeer32 avatar Jul 24 '22 17:07 alikabeer32

@Mathieu2301 Here is something I came up with. It is probably not ideal but works fine for small disruptions of a few seconds. You can add more logic to this like repeatedly attempting to reconnect. Forgive me if some brackets are misplaced.


let client;
let chart; 

function connect(){
 client = new TradingView.Client(); // Creates a websocket client

 chart = new client.Session.Chart(); // Init a Chart session

chart.setMarket('SOLUSDT', { // Set the market
  timeframe:'15',
});


client.onConnected(() => {
  console.log("client connected!");  

});

client.onDisconnected(() => {
  console.log("client disconnected!");

  setTimeout(() => {
    connect();
  }, 10000);
});
}

connect();


alikabeer32 avatar Jul 25 '22 14:07 alikabeer32