Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api is not reconnecting to websocket #46

Open
eringiso opened this issue Dec 22, 2020 · 2 comments
Open

api is not reconnecting to websocket #46

eringiso opened this issue Dec 22, 2020 · 2 comments

Comments

@eringiso
Copy link

how to reconnect to websocket i'm not passing the websocket connection as described in the docs
passing only app_id so that api can auto reconnect but still api is disconnecting after an hour and the program exits
here is my code

const api = new DerivAPI({
    app_id
});


fx_symbols = ['frxAUDCAD', 'frxAUDJPY', 'frxAUDUSD', 'frxEURAUD', 'frxEURCAD', 'frxEURCHF', 'frxEURGBP', 'frxEURJPY', 'frxEURNZD', 'frxEURUSD','frxNZDUSD', 'frxUSDCAD', 'frxUSDCHF', 'frxUSDJPY',]

fx_candles_1m = {
    "frxAUDCAD": [],
    "frxAUDJPY": [],
    "frxAUDUSD": [],
    "frxEURAUD": [],
    "frxEURCAD": [],
    "frxEURCHF": [],
    "frxEURGBP": [],
    "frxEURJPY": [],
    "frxEURNZD": [],
    "frxEURUSD": [],
    "frxNZDUSD": [],
    "frxUSDCAD": [],
    "frxUSDCHF": [],
    "frxUSDJPY": [],
}

(async () => {
    fx_symbols.map(x => { getAllCandles(x, 60).catch(err => console.log(err)) })
})()

async function getAllCandles(symbol, tf) {
    candle_data = await api.candles({ symbol: symbol, granularity: tf, end: "latest", style: "candles", subscribe: 1 })
    candle_data.onUpdate(data => {
        if (data.raw.epoch === data.raw.open_time + tf - 1) {
            // console.log("The candle data for " + symbol + " is : ", data.raw);
            o = data.raw.open
            h = data.raw.high
            l = data.raw.low
            c = data.raw.close
            epoch = data.raw.epoch + 1
            const x = [o, h, l, c, epoch]
            const { granularity, id, open_time, pip_size, symbol, ...noA } = data.raw

            noA['epoch'] = noA['epoch'] + 1
            fx_candles_1m[symbol].push(x)
            console.log(symbol, vol_candles_1m[symbol]);

            const open = fx_candles_1m[symbol].map(d => d[0])
            const high = fx_candles_1m[symbol].map(d => d[1])
            const low = fx_candles_1m[symbol].map(d => d[2])
            const close = fx_candles_1m[symbol].map(d => d[3])

            if (fx_candles_1m[symbol].length > 14) {
              let rsi = RSI.calculate({ period: 14, values: close })
              console.log("The RSI value of " + symbol + " is ", rsi.slice(-2))
            }


            if (fx_candles_1m[symbol].length > 15) {
                fx_candles_1m[symbol].shift()
            }
            return Promise.resolve([symbol, data.raw])

        }
    })
}

what should i do to reconnect if api is disconnected

@iitsuraj
Copy link

iitsuraj commented Oct 7, 2021

As mentioned in the doc, it will automatically reconnect. But it is not happening. To prevent automatically close, you have to send a ping after each 30 sec.

To reconnect -

...
api.basic.onClose().subscribe(()=>api.basic.connect());
...

Now you can close this issue

References - https://binary-com.github.io/deriv-api/#derivapibasicclosehandler

@vinifr
Copy link

vinifr commented Jun 18, 2022

Hello. I am sending {"time":1,"req_id":3,"passthrough":{}} every 30 seconds like the Binary website does, but yet my websocket client is disconnected by the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants