Check if user is live nodejs

Hi, so i want to check if a specific user is live or not. I used a while loop to check every second, but everytime the user goes live, the api request executes and finds the user but then right after in the next request, there is no data found wich then my code thinks the user has gone offline again.

async function getStream(){
    while(true){
        const streams = await twitch.getStreams({ channel: "testname" });
        if(streams.data.length > 0){
            console.log("online")            
        }else{
            console.log("offline")
        }
        await sleep(1000)
    }
}


getStream();

so the output is this:

offline
offline
offline

  • online
  • offline (while user is live)
    online
    online
    etc

Why is this? thanks in advance

The API is cached, and requesting the same resource excessively fast (such as every second) you run the risk of hitting a fresh cache on one request, and a stale cache on a subsequent request, which is why it may appear they are going online/offline.

It is recommended to not poll for the same data faster than once per minute.

1 Like