简体   繁体   中英

How to listen to MongoDB changes in Node.js?

I was looking for an answer for two days now and finally made the decision to just ask about the following question in here:

I was working with Googles Firebase / Firestore as backend for my Flutter application and everything worked so far.

Now I came to the decision to write my very own API with "the help" of Node JS and Express. I am already able to store user data inside my MongoDB database using http requests. I wrote different service files in Dart that send requests and so on. Now with Firestore I was able to setup some streams that listen to the documents based on the users ID. For example I listened to direct messages and group messages associated to that user and immediately update the UI (chat) when there was a new message.

I was wondering if there is a possibility to achieve the same thing with my self written API. I read some things about "Change Streams" and so on, but couldn't really figure out how to implement such a Stream with Node JS and listen to it on the Dart Frontend as soon as the user uses the application. Same thing eg whenever a user I refer to in some way, uploads a new post.

There are two possible solutions to your problem

  1. When it comes to listening to the changes, connect MongoDb to Flutter Directly using this official Guide https://www.mongodb.com/docs/realm/sdk/flutter/realm-database/react-to-changes/
  2. second if your Nodejs backend is not that deep and you only deal with CRUD operation then you completely shift of MongoDB and Flutter Collection, without making any hassle for Nodejs Backend

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM