Yup, I think Matt's approach is cleaner and it's how I'm doing it. In most cases you want to drive your long calculations off database changes (I assume that's a pretty safe assumption) so the pattern is somewhere in your client/something.js file:
// Launch this when Meteor is initialized - only runs on the client
Meteor.startup(function() {
var Messages = new Meteor.Collection('messages')
// Auto-subscribe so we automatically react to changes on a collection subscription
Meteor.autosubscribe(function() {
// Say the subscription depends on some setting like 'currentChatRoom'
var currentChatRoom = Session.get('currentChatRoom')
// Since this is in a reactive context, changes to the session's currentChatRoom will automatically rerun this subscription
Meteor.subscribe('messages',currentChatRoom,function() {
// Do something expensive with the current chat room's current list of messages
// if the collection changes rapidly underneath you, this will keep getting re-run which could be expensive, for an actual chat app
// you may need to incrementally update the stats rather than calculate fresh stats on each list update... at least the stats only
// get run once per messages collection change rather than in each place that the stats appear in the page templates.
var messages = Messages.find({}}
var messageStats = longMetricsCalculation(messages)
Session.set('chatRoomStats',messageStats)
}
}
On the server you'd do something like server/something.js - only runs on the server
Meteor.startup(function() {
var Messages = new Meteor.Collection('messages')
Meteor.publish('messages',function(currentChatRoom) {
return Messages.find({room,currentChatRoom})
}
}
Then in your template javascript file (only on the client):
Template.<nameOfTemplate>.stats = function() {
return Session.get('chatRoomStats')
}
And in your template you just use the expensive stat (foo.html)
<h3>Trending</h3>
<ul>
{{each stats.trendingTopics}}
<li>{{title}}</li>
{{/each}}
Meteor will make sure all the magic runs behind the scenes to link up changes in the session to changes in the subscription, to changes in the HTML. It can get a little rube goldberg in design since you have to be thinking about "ok this session change, triggers this collection change, which triggers this session change, which triggers this template update" but it's really all about managing application state via events and session/collection state.
I hope that explanation helps more than confuses. :)
-iain