0

I'm working on a chat project, NodeJS has to wait 25 secs for new data from MongoDB, any alternatives?

while(true) {
    db.messages.find({
    ...
    }).then(result) => {
        if (result.length > 0) {
            return result;
        }
    })
}

UPDATE:

let secs = 0;
const iv = setInterval(() => {
        secs++;

        db.messages.find({
        ...
        }).then(result) => {
            if (result.length > 0) {
                clearInterval(iv);

                res.json(result);
                return;
            }
        });

        if (secs === 25)
           clearInterval(iv);
}, 1000);
1
  • check change stream in MongoDB Commented Aug 27, 2019 at 9:06

1 Answer 1

2

take a look at changeStream

const collection = db.collection('messages');
const changeStream = collection.watch();
changeStream.on('change', next => {
  // process next document
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.