It looks like you want to be notified of new / updated / deleted objects in the database. This is not possible with a Mongodomb without the slightest deception. I assume you read about reading oplogs using tail cursors, and polling is always an absolute last resort. I have never tried them because they seem a bit limited (cannot use them in common db environments) and are unreliable - not to mention complicated setup (requires a set of replicas) and are subject to change at any time in the future without warning. For example, some popular mongo-watch library is no longer supported in better alternatives).
DB mutation events are implemented in some databases: Postgres implements triggers, and RethinkDB actually pushes you to change. If you can switch to something like RethinkDB, that would be ideal.
If not, my best advice for you is to set a service level in front of your db through which all traffic should go. Client applications can connect to these services through sockets (which is trivial with socket.io - implemented in almost every language). Each time your service level processes updates, inserts, or deletes, you can pass these events on to someone who is currently connected.
Limitations with this approach
- All db messages must pass through the service layer.
Cautions with this approach
- If something updates the database directly, you will not see these changes immediately. You will have to request db again. Not the end of the world.
Benefits of using this approach
- This is better, more effective and in real time than a survey.
- You have a level of service that can do much more business data with your data, for example, when transferring data, changing data, checking data, sending letters, registering, updating other data sources, etc .;)
- This is a paradigm that works with any language, any db.
- There are lightweight frameworks that already implement this architecture. FeathersJS is my favorite. You really have to check it out. If you can use NodeJS, you should at least make it clear how feathers work.
source share