Question

Redis gives ECONNREFUSED halfway through an API call

Hi!

I’m working with an API and I send out 10 requests each a few seconds, then pulls the responses, merges the JSON, then sends it to the redis client. halfway through the API calls redis gives the following error


Error: Redis connection to <server_ip>:6379 failed - connect ECONNREFUSED <server_ip>:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) 

relevant node.js code


async function getSeasonsForCompetitions(competitions) {
        let totalJSON = []
        try {
                for (let i = 0; i < competitions.length; i++) {
                        await delay(1000)
//HALFWAY THROUGH THIS FUNCTION THROWS THE ERROR
                        let season = await getJSON(`https://api.sportradar.us/soccer/trial/v4/en/seasons/sr:season:${competitions[i]}/schedules.json?api_key=${process.env.API_KEY_SOCCER}`)

                        console.log(`${competitions.length - i } remain`)
                        totalJSON.push(season)
                }
                return totalJSON
        } catch(e) {
                console.log("error", e)
                return totalJSON
        }
}


async function setSchedule() {
        let soccerJSON = await getScheduleSoccer()
        console.log("got soccer object")
        let nbaJSON = await getScheduleNBA()
        console.log("got NBA object")
        let nflJSON = await getScheduleNFL()
        console.log("got nfl object")
        let nhlJSON = await getScheduleNHL()
        console.log("got nhl object")
        let mlbJSON = await getScheduleMLB()
        console.log("got mlb object")
        let f1JSON  = await getScheduleF1()
        console.log("got f1 object")
        let nbaDate = moment().format('LL') + "-NBA"
        let nflDate = moment().format('LL') + "-NFL"
        let nhlDate = moment().format('LL') + "-NHL"
        let mlbDate = moment().format('LL') + "-MLB"
        let f1Date  = moment().format('LL') + "-F1"
        let soccerDate = moment().format('LL') + "-Soccer"
        let redisArr = [
                nbaDate,
                JSON.stringify(nbaJSON),
                nflDate,
                JSON.stringify(nflJSON),
                nhlDate,
                JSON.stringify(nhlJSON),
                mlbDate,
                JSON.stringify(mlbJSON),
                f1Date,
                JSON.stringify(f1JSON),
                soccerDate,
                JSON.stringify(soccerJSON)
        ]
        console.log("writing all objects to db")
        redisClient.mset(redisArr, (err, reply) => {
                if (err) {
                        console.log(err)
                } else {

                        setLatestJSONKeys().then((obj) => {
                                process.exit();
                        })
                }
        })
}

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello there,

You can make sure that the remote server is online and also that it accepts connections on port 6379 as well.

You can use telnet in order to check if you can access the port:

telnet DropletIPaddress 6379

If the connection is successful you will get the output - “Connected” or similar to this one.

Hope that this helps! Regards, Alex