2

I have a MongoDB database, Node.js server and User Interface.

The connection between the three is working and data is being sent between them.

I am creating a real-time web application and I am using a setTimeout() feature to request data from database via the client's webpage every 4 seconds.

BUT the data that is coming back is the same everytime and isnt updating from the database. It is acting as if there is only one MongoDB session and just sending the same set of data that is collected from when the server starts.

On the Node.js server code, I made sure that a connection is opened and close once the query has completed but it is collecting the same set of data every time and no new data that has come into the database. The only way I can get it to update the data from the database is to turn the server on and off to reconnect to the database.

Would using some thing like socket.io be better for the real time requests or is it possible to do real time data just with a setTimeout() feature on the client side and normal MongoDB collection queries? Is there a way to constantly refresh the connection to the database?

  • 1
    Why are you opening and closing DB connections everytime you make a query? You should connect to the DB only once and that should be when you start your application. As for the second part of the question, socket.io would be an ideal solution. Also, with socket.io you can only update your interface if there is an update on the database, instead of updating everything once every 4 seconds. There are alternatives to MongoDB for real-time applications, an example would be [RethinkDB](https://rethinkdb.com/) you might want to check that, but as I said it can be done with socket.io as well. – nick Aug 26 '20 at 20:00
  • Thanks @nick I opened and closed the MongoDB connection for every query as it seemed to show that as general practice in their documentation and theoretically get the latest information on each connect. From the looks of it RethinkDB may be a good way forward, it looks very similar to MongoDB but how is it different and support real-time data better? – Helen Johnson Aug 26 '20 at 20:14
  • 1
    If I understand correctly, it supports real-time data by impementing WebSockets so that you don't need to hassle with socket.io. More detailed (and accurate) information can be found in their [faqs](https://rethinkdb.com/faq). Also, instead of using MongoDB directly you can use [mongoose](https://www.npmjs.com/package/mongoose), which is some sort of wrapper to the mongoDB api. In my opinion, most of the time it is faster and easier to use mongoose instead of mongoDB api. – nick Aug 26 '20 at 20:34
  • 1
    Check out this [thread](https://stackoverflow.com/q/38693792/5358219) and this [thread](https://stackoverflow.com/q/32906467/5358219) about new connections. With mongoose you don't need to open new connection for every query. It automatically handles it via connections pool. – nick Aug 26 '20 at 20:45
  • 1
    Awesome, I'll have a look to see if mongoose solves the problem if not I'll rejig things to RethinkDB. Thanks!! – Helen Johnson Aug 26 '20 at 20:50
  • 1
    @HelenJohnson "I opened and closed the MongoDB connection for every query as it seemed to show that as general practice in their documentation and theoretically get the latest information on each connect." - Nope, they mention that the way to do is by connection pooling, yes its not explain inside their documentation but rather the developer path or their videos. – Koodies Aug 27 '20 at 02:21

0 Answers0