This is with meteor 1.9.
I thought I’d introduce a little reactivity into my app so I started experimenting with Mongodb change streams but I don’t seem to be getting any notifications when running on my local db started by meteor.
The docHandler callback is never invoked even when I make manual changes to the document that the $match step matches.
Here’s my code - I stripped out some stuff that wasn’t interesting:
// Inspired by https://github.com/kschingiz/meteor-publish-change-streams/blob/master/publish-change-streams.js
Meteor.publish("Candidate.completionState", function(this, candidateId: any) {
check(candidateId, String);
const sub = this;
const userId = sub.userId;
const selector = {
_id: candidateId
};
const pipeline: AggregationStep[] = [
{
$match: selector
}
];
const initial =
collection.findOne(selector)
).await();
if (!initial) {
throw new Meteor.Error("not-found");
}
sub.added("CandidateCompletions", candidateId, initial);
sub.ready();
console.log("ready");
const changeStream = collection.watch(pipeline, {
fullDocument: "updateLookup"
});
const publishRunner = () => {
const docHandler = (doc: any) => {
const { fullDocument, operationType } = doc;
console.log("change happened");
console.log(JSON.stringify(doc));
};
console.log("waiting for changestream");
changeStream.on("change", docHandler);
};
publishRunner();
this.onStop(() => {
console.log("closing changestream");
changeStream.close();
});
});
From what I can see, the local meteor db is setup as a replica so it could work and the initial synchronous findOne query works fine so the collection connection is working.
What am I missing?