For prototyping reasons, I want to implement a kind of “newsticker” on the homepage of my app. The homepage shall always show the 3 latest news.
To fake this, I’ve implemented a Meteor.method that fills a ticker collection in cyclic interval, i.e. it inserts a fake ticker message to the news collection every 5-10 seconds. This works, and I can see the collection inserts on my console log and in the collection itself.
I then created a publication like this:
Meteor.publish('latestNews', function() {
var cursor = news.find({
$query: {},
$orderby: {
updatedAt: -1
}
}, {
limit: 3
});
return cursor;
});
In the client template, I’ve subscribed to this like this:
Template.home.onCreated(function() {
var instance = this;
instance.autorun(function() {
var subscription = instance.subscribe('latestNews');
if (subscription.ready()) {
}
});
});
and in a helper I call a find
on the news collection to actually retrieve the documents.
latestNews: function() {
var latestNews = news.find({}, {
sort: {
updatedAt: -1
},
limit: 3
});
return latestNews;
}
According to this screencast, the publication should be reactive because cursors are automatically observed.
In principle, this works, my ticker updates and shows the 3 most recent entries for a while. But suddenly, it stops and isn’t updated anymore. The same happens if a hot code reload takes place. As soon as I restart the server, the ticker is working again, until it stops again after a while.
So I am wondering what is causing the reactivity to stop after a while? Did I miss something?