Hey everyone!
I’ve been working on a long-awaited feature: support for MongoDB Change Streams in Meteor!
This new capability allows you to listen to real-time events directly from MongoDB (like inserts, updates, and deletes), using either a new watchChangeStream method or by enabling it inside the existing observeChanges API. All of this happens without relying on polling
I just opened a PR in our github.
What’s working so far
Mongo.Collection.watchChangeStream
You can now open a MongoDB Change Stream directly on a server-side collection:
// server.js
const changeStream = MyCollection.watchChangeStream([
{ $match: { operationType: 'insert' } }
]);
changeStream.on('change', (change) => {
console.log('Detected change event:', change);
});
changeStream.on('error', (err) => {
console.error('Change stream error:', err);
});
observeChanges
Having an easy migration in mind, you will be able to enable Change Streams with the changeStreams: true option. Events will be automatically mapped to the usual added, changed, and removed callbacks:
// server.js
const handle = MyCollection.find().observeChanges({
changeStreams: true, // <-----NEW FLAG
added: (id, fields) => {
console.log('Added', id, fields);
},
changed: (id, fields) => {
console.log('Changed', id, fields);
},
removed: (id) => {
console.log('Removed', id);
},
onError: (err) => {
console.error('observeChanges error:', err);
}
});
Still a Work In Progress
This is the first working implementation, and while it’s already functional, I’d love to hear your feedback on the developer experience (DX), naming, ergonomics, and potential edge cases.
Let me know your thoughts, suggestions, or use cases you’d like this to support!
Thanks