1

I'm a newbie to Nodejs so I hope that I can describe my problem well.

I have a local Nodejs express app server that runs on a windows machine by 'node app.js' command and listening to clients connections by socket.io (version 2.4.1 - as the client's) and preforming a data polling on every 5 seconds from a local db in order to detect changes in that db and notify all connected clients with that new data.

The problem is that in some point, server stops responding to any new/existing clients and stops preforming it’s polling logic.

The polling logic is built from a while loop which does that with a Promised timeout of 5sec and notify clients just when there is a new data (I also tried to use setInternal method instead without a Promise - which caused the same issue):

async function startServerLoopAsync() {
    while (true) {
        try {
            let isDataChanged = checkAndUpdateData();
            if (!isDataChanged)
                continue;

            serverManager.NotifyClients(data);
        } catch (ex) {
            console.error('error while updating data! ' + ex)
        }

        await new Promise(resolve => setTimeout(resolve, 5000))
    }
}

The startup code:

const promise = startServerLoopAsync();
serverManager.StartListening(8000);

This problem can happen 48 hours or even 30 minutes after server is up. Also, when server is running up again, it gets thousands of socket connections (when there are like 1 or 2 clients that tries to connect to it) which takes up to 2GB of memory until server is crashing from it:sockets connections on startup

What I'm doing wrong? ;(

Raz Vaknin
  • 31
  • 4
  • 1
    I recommend you to emit the event at the moment you change the data to the DB, doing this you don't need use the logic to observe the changes, emit the event when the change was commited will be better. –  Apr 29 '22 at 20:09
  • And if you really need to poll, you can try using `setInternal` method instead of while true loop. – Prashant Ghimire Apr 29 '22 at 20:13
  • I’m polling instead because of some constraints.. As for the setInterval method - Ive already tried it and it caused to the same issue :/ – Raz Vaknin Apr 29 '22 at 20:33
  • Have you found a solution? I have a similar issue but with python and nats messages. – Skezo Apr 30 '22 at 22:10

0 Answers0