Mathieu2301 / TradingView-API

📈 Get real-time stocks from TradingView

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How do I reconnect to client after my internet reconnects?

alikabeer32 opened this issue · 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);
});

@Mathieu2301 can you please help me out here?

@Mathieu2301 can you please help me out here?

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

@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();