I want a mongodb query to return snapshots like how this firebase query returns:
Stream<QuerySnapshot<Map<String, dynamic>>> getChatMessages(String chatRoomId) {
return _firestore
.collection('chats')
.doc(chatRoomId)
.collection('messages')
.orderBy('timestamp', descending: true)
.snapshots();
}
I tried creating a stream in node where everytime there is a change in user data i am console logging it. How do I create a stream in flutter so that I can use it in a StreamBuilder to listen to real-time updates?
const getStream = asyncHandler (async (req, res) => {
try {
const { id } = req.params;
const user= await User.findOne({ _id: id }).exec(); // Execute the query to get the actual document
const changeStream = user.collection.watch(); // Get the collection from the document and create a change stream
changeStream.on('change', (data) => {
console.log(data);
});
res.status(200).json({ message: 'success' });
} catch (error) {
console.log(error);
res.status(500).json({ message: 'error' });
}
});
New contributor
Hasti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1